-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
53 lines (44 loc) · 1.33 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
/*
* 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 { connectToDb } = require('./lib/mongo')
const app = express()
const port = process.env.PORT || 8000
/*
* Morgan is a popular 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."
})
})
connectToDb(function () {
app.listen(port, function () {
console.log("== Server is running on port", port)
})
})