-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
50 lines (40 loc) · 1.17 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
'use strict';
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const models = require('./server/models');
const usersRoutes = require('./server/routes/users');
// Our server listening port
let port = process.env.NODE_PORT || 3000;
// Create our express application
const app = express();
// Accept bodies url and json encoded
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Set static file directory
app.use(express.static(path.join(__dirname, '/public')));
app.set('views', path.join(__dirname, '/server/views'));
// Serve index file
app.set('view engine', 'ejs')
app.get('/', (req, res) => {
res.render('index', {
title: 'Hello world!',
message: 'Good to see you'
});
});
// Set API routes
app.use('/users', usersRoutes);
// Synchronize database with the previously declared models
models.sequelize.sync({
alter: false
}).then((db) => {
console.log('Database connected');
}).catch(err => {
console.log('Error connecting database:', err);
});
// Start listening to external http requests
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});