-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (47 loc) · 1.73 KB
/
server.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
'user strict'
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var morgan = require('morgan');
var app = express();
let port = 3080;
let config = require('config'); //we load the db location from the JSON files
let options = {
useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false
};
//db connection
mongoose.connect(config.DBHost, options).then(() => {
console.log("The connection to the camillion database was successful.")
})
.catch(err => console.log(err));;
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
//don't show the log when it is test
if (config.util.getEnv('NODE_ENV') !== 'test') {
//use morgan to log at command line
app.use(morgan('combined')); //'combined' outputs the Apache style LOGs
}
//load routes
var news_routes = require('./routes/homenews');
//parse application/json and look for raw text
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(bodyParser.json({ type: 'application/json' }));
//cors
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Request-Method');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
res.header('Allow', 'GET, POST, OPTIONS, PUT, DELETE');
next();
});
//routes
app.use('/api', news_routes);
//starting server
app.listen(port, () => {
console.log("Server running on http://localhost:" + port);
});
console.log("Listening on port " + port);
//export
module.exports = app;