-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (36 loc) · 1.09 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
/**
* Adviz Server Class
*/
// Dependencies
const express = require('express');
const mongoDBClient = require('./database.js')
const connectionString = 'mongodb://localhost:27017/adviz';
// Routers
const homepageRouter = require('./routes/index');
const contactsRouter = require('./routes/contacts');
const usersRouter = require('./routes/users');
// Parser
const bodyParser = require('express');
// Initialize Express
const app = express();
// Static files
app.use(express.static(__dirname + '/public'));
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json())
// Routes
app.use('/', homepageRouter);
app.use('/contacts', contactsRouter);
app.use('/users', usersRouter);
// Start the DB Connection and Server -> listen on port 3000
mongoDBClient.connectDB(connectionString, function(err) {
if (err) {
console.log('Error: Unable to connect to MongoDB.')
process.exit(1)
} else {
app.listen(3000, function() {
console.log('Server connection established! Listening on port 3000...')
})
}
})