-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
57 lines (48 loc) · 1.45 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
/*
* This require() statement reads environment variable values from the file
* called .env in the project directory. You can set up the environment
* variables in that file to specify connection information for your own DB
* server.
*/
require("dotenv").config();
const express = require("express");
const morgan = require("morgan");
const api = require("./api");
const sequelize = require("./lib/sequelize");
const app = express();
const port = process.env.PORT || 8000;
/*
* Morgan is a popular request logger.
*/
app.use(morgan("dev"));
app.use(express.json());
/*
* 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.",
});
});
/*
* Start the API server listening for requests after establishing a connection
* to the MySQL server.
*/
sequelize.sync().then(function () {
app.listen(port, function () {
console.log("== Server is running on port", port);
});
});