-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (61 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Process.env
*/
require('dotenv').config();
const express = require('express');
const app = express();
global.__basedir = __dirname;
/**
* Headers
*/
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Authorization, Origin, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "POST, GET, DELETE");
res.header("Access-Control-Allow-Credentials", true);
next();
});
/**
* Mongoose
*/
var mongoose = require('mongoose');
var db = mongoose.connection;
mongoose.connect(`mongodb://${process.env.DB_HOST}/${process.env.DB_NAME}`);
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Connected to MongoDB");
if(process.env.DB_NAME == 'test'){ //if TxTag API is running as test, clear the test database each time!.
mongoose.connection.dropDatabase();
}
});
/**
* Passport
*/
var passport = require('passport');
require('./config/passport')(passport);
app.use(passport.initialize());
/**
* Body/Payload Parser
*/
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
/**
* Express validator
*/
var expressValidator = require('express-validator');
var customExpressValidators = require('./validators/express-custom-validators');
app.use(expressValidator(customExpressValidators));
var privateApiRoutes = require('./routes/private/private');
/**
* Routing
*/
app.use('/private', privateApiRoutes);
var server = app.listen(3000, () => console.log('Example app listening on port 3000!'))
// catch 404 and forward to error handler
app.use(function(req, res, next) {
res.status(404).json("Invalid Endpoint");
});
module.exports = server;