-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (44 loc) · 1.28 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
'use strict';
const express = require('express');
const morgan = require('morgan');
const cors = require('cors');
const { PORT } = require('./config');
const notesRouter = require('./routes/notes');
const foldersRouter = require('./routes/folders');
const tagsRouter = require('./routes/tags');
// Create an Express application
const app = express();
// Log all requests
app.use(morgan('dev'));
// Create a static webserver
app.use(express.static('public'));
// Enable CORS support
app.use(cors());
// Parse request body
app.use(express.json());
// Mount router on "/api"
app.use('/api/notes', notesRouter);
app.use('/api/folders', foldersRouter);
app.use('/api/tags', tagsRouter);
// Custom 404 Not Found route handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// Custom Error Handler
app.use((err, req, res, next) => {
if (err.status) {
const errBody = Object.assign({}, err, { message: err.message });
res.status(err.status).json(errBody);
} else {
console.error(err);
res.status(500).json({ message: 'Internal Server Error' });
}
});
// Listen for incoming connections
app.listen(PORT, function () {
console.info(`Server listening on ${this.address().port}`);
}).on('error', err => {
console.error(err);
});