-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (36 loc) · 1.23 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
// get dependencies
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// parse requests
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
//Enable CORS for all HTTP methods
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Configuring the database
const config = require('./config.js');
const mongoose = require('mongoose');
require('./product.routes.js')(app); //Add route file here
mongoose.Promise = global.Promise;
// Connecting to the database
mongoose.connect(config.url, {
useNewUrlParser: true
}).then(() => {
console.log("Successfully connected to the database");
}).catch(err => {
console.log('Could not connect to the database. Exiting now...', err);
process.exit();
});
// default route
app.get('/', (req, res) => {
res.json({"message": "Welcome to ZeptoBook Product app"});
});
// listen on port 3000
app.listen(config.serverport, () => {
console.log("Server is listening on port 3000");
});