-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (41 loc) · 1.2 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
const express = require('express');
const morgan = require('morgan');
const api = require('./api');
const sequelize = require('./lib/sequelize')
const { rateLimit } = require('./lib/rate_limiting')
const app = express();
const port = process.env.PORT || 8000;
/*
* Morgan is a popular request logger.
*/
app.use(morgan('dev'));
app.use(express.json());
app.use(express.static('public'));
// Rate limiting
app.use(rateLimit)
/*
* All routes for the API are written in modules in the api/ directory. The
* top-level router lives in api/index.js. That's what we include here, and
* it provides all of the routes.
*/
app.use('/', api);
app.use('*', function (req, res, next) {
res.status(404).json({
error: "Requested resource " + req.originalUrl + " does not exist"
});
});
/*
* This route will catch any errors thrown from our API endpoints and return
* a response with a 500 status to the client.
*/
app.use('*', function (err, req, res, next) {
console.error("== Error:", err)
res.status(500).send({
err: "Server error. Please try again later."
})
})
sequelize.sync().then(function () {
app.listen(port, function() {
console.log("== SERVER is running on port", port);
});
})