-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (62 loc) · 1.6 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
61
62
63
64
65
66
67
68
69
70
'use strict';
const express = require('express');
const fileUpload = require('express-fileupload');
// Constants
const HOST = process.env.HOST;
const PORT = process.env.PORT;
const db = require('./app/models');
const dbConfig = require('./app/config/mongodb.config');
const Role = db.role;
// DB
db.mongoose
.connect(`${dbConfig.URI}/${dbConfig.DB}`, {
authSource: 'admin',
user: `${dbConfig.USER}`,
pass: `${dbConfig.PASS}`,
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('Successfully connect to MongoDB.');
initial();
})
.catch((err) => {
console.error('Connection error', err);
process.exit();
});
// App
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(fileUpload());
app.get('/', (req, res) => {
res.send('Hello, now we add pagination feature in our application');
});
//routes
require('./app/routes/item.routes')(app);
require('./app/routes/user.routes')(app);
require('./app/routes/route.routes')(app);
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
function initial() {
Role.estimatedDocumentCount((err, count) => {
if (!err && count === 0) {
new Role({
name: 'user',
}).save((err) => {
if (err) {
console.log('error', err);
}
console.log("added 'user' to roles collection");
});
new Role({
name: 'admin',
}).save((err) => {
if (err) {
console.log('error', err);
}
console.log("added 'admin' to roles collection");
});
}
});
}