-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
29 lines (24 loc) · 809 Bytes
/
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
const express = require('express');
const mongoose = require('mongoose');
const bodyParse = require('body-parser');
const cors = require('cors');
const app = express();
const bookRouter = require('./route/book');
const port = process.env.PORT || 3000;
if (process.env.NODE_ENV !== 'production') require('dotenv').config();
app.use(bodyParse.json());
app.use(cors());
//Create server port
app.listen(port, () => console.log(`Server listen on port ${port}`));
//Connect to DB
const db = mongoose.connection;
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
});
//If connected to DB
db.once('open', () => console.log(`Successfully connected to MongoDB`));
//If failed connect to DB
db.on(`error`, (err) => console.log(err));
//Route
app.use('/books', bookRouter);