-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
35 lines (29 loc) · 1.13 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
var express = require('express'),
bodyParser = require('body-parser'),
cors = require('cors'),
glob = require('glob'),
path = require('path'),
mongoose = require('mongoose');
var app = express();
var router = express.Router();
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.use(cors({ origin: '*' }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/api', router);
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/app-api-db', { useNewUrlParser: true });
mongoose.set('debug', true);
glob.sync(path.join(__dirname, '**/models/*.model.js')).forEach(function (file) {
require(path.resolve(file))(mongoose);
});
glob.sync(path.join(__dirname, '**/routes/*.route.js')).forEach(function (file) {
require(path.resolve(file))(router);
});
var server = app.listen(process.env.PORT || 3000, function () {
console.log('Listening on port ' + server.address().port);
});