-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
70 lines (59 loc) · 1.8 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
// ******************************************
// INITIALIZATION
// ******************************************
// Dependencies
const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');
// Configuration
const app = express();
const port = process.env.PORT || 3000;
// Routes
const angular = require('./node_src/routes/angular');
const api = require('./node_src/routes/api');
// Connect to database via mongoose
require('./node_src/config/db');
// View engine
app.set('view engine', 'html');
app.set('views', 'dist');
// ******************************************
// MIDDLEWARE
// ******************************************
// Morgan
app.use(logger('dev'));
// Body-Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// ******************************************
// ROUTES
// ******************************************
// Route for Angular
app.use('/', angular);
// Route for APIs go here
app.use('/api', api);
// ******************************************
// API ERROR HANDLER
// ******************************************
// Error handler for 404 - Page Not Found
app.use(function (req, res, next) {
let err = new Error('Not Found');
res.status(404).json({
status: 404,
message: err.message,
name: err.name
});
});
// Error handler for all other errors
app.use(function (err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500).json({
status: 500,
message: err.message,
name: err.name
});
});
// ******************************************
// SERVER START
// ******************************************
app.listen(port, () => console.log(`Server started on port ${port}`));