-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (37 loc) · 1.27 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
let express = require('express'),
mongoose = require('mongoose'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
productRoutes = require('./routes/product-routes');
// Load Config
let config = require('./config').get(process.env.NODE_ENV);
// Express
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.text());
app.use(bodyParser.json({type: 'application/json'}));
// Mongo DB options
let options = {
server: {socketOptions: {keepAlive: 1, connectTimeoutMS: 30000}},
replset: {socketOptions: {keepAlive: 1, connectTimeoutMS: 30000}}
}
// Mongo DB Connection
mongoose.connect(config.database, options);
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
// Don't log on test env
if (process.env.NODE_ENV !== 'test') {
app.use(morgan('combined'));
}
// Routes
app.get("/", (req, res) => res.json({message: "Welcome to the API"}));
app.route("/products")
.get(productRoutes.getProducts)
.post(productRoutes.storeProduct);
app.route("/products/:id")
.get(productRoutes.getProduct)
.delete(productRoutes.deleteProduct)
.put(productRoutes.updateProduct);
app.listen(config.port);
module.exports = app; // for testing