-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
61 lines (48 loc) · 1.53 KB
/
app.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
/**
* Created by Yido on 2/25/2017.
*/
/* LOAD ALL Module Dependencies */
var express = require('express');
var bodyParser = require('body-parser');
var debug = require('debug')('anwani-project');
var mongoose = require('mongoose');
var validator = require('express-validator');
var cors = require('cors')
var config = require('./config');
var router = require('./routes');
var authenticate = require('./lib/authenticate');
//~ Connect to MongoDb ~//
mongoose.connect(config.MONGODB_URL);
//~ listen to connection event ~//
mongoose.connection.on('connected',function mongodbConnectionListener(){
debug('Mongodb Connected successfully');
});
mongoose.connection.on('error',function mongodbErrorListener(){
debug('Connection to Mongodb Failed!!');
});
//~ Configuration ~//
//~ Initialize app ~//
var app = express();
//~ Authentication Middleware ~//
app.use(authenticate().unless({
path: ['/users/login','/users/signup']
}));
//~ Set Middleware ~//
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json({limit: '50mb'}));
app.use(cors());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
//~ Set Validator ~//
app.use(validator());
//~ Set Routes ~//
router(app);
//~ Listen to HTTP Port ~//
app.listen(config.HTTP_PORT,function connectionListener() {
debug('API Server is running on port %s',config.HTTP_PORT);
});