-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (38 loc) · 1.21 KB
/
server.js
File metadata and controls
51 lines (38 loc) · 1.21 KB
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
const mongoose = require('mongoose');
const express = require('express');
const bodyParser = require('body-parser');
const logger = require('morgan');
const jwt = require('./_helpers/jwt');
const errorHandler = require('./_helpers/error-handler');
const swaggerDoc = require('./swaggerDoc');
const {
API_PORT,
DB_URL
} = process.env
const app = express();
// var cors = require('cors');
const router = express.Router();
// connect to mongo db atlas server
const dbRoute = DB_URL
// connect backend code to database
mongoose.connect(dbRoute,
{
useNewUrlParser: true,
useCreateIndex: true
}
);
let db = mongoose.connection;
db.once("open", () => console.log("connected to the database"));
// check if connection with the db is successful
db.on("error", console.error.bind(console, "MongoDB connection error:"));
swaggerDoc(app);
//app setup
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(jwt());
const apiUrl = "/api/v1";
//append /api/v1 to http requests
app.use(`${apiUrl}/users`, require('./user/user.controller'));
app.use(errorHandler);
// launch the backend into a port
app.listen(API_PORT, () => console.log('Server listening on port ' + API_PORT));