-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (29 loc) · 838 Bytes
/
server.js
File metadata and controls
35 lines (29 loc) · 838 Bytes
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
const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const routes = require('./routes');
const errorHandling = require('./middleware/error.js');
const port = process.env.PORT || 3001;
const app = express();
app.use(logger('dev'));
// converts request body to js object
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// requests flow to appropriate place
app.use('/', routes);
// unrouted requests fall through to here
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// all routes with errors fall through to here
app.use(errorHandling);
// start server
app.listen(port, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Listening on port ${port}`);
});