-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (55 loc) · 1.77 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
59
60
61
62
63
64
65
66
/*
* 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 { redisClient } = require('./lib/redis')
const app = express()
const port = process.env.PORT || 8000
/*
* Run mongoDB server:
* docker run -d --name mongo-server -p "27017:27017" -e "MONGO_INITDB_ROOT_USERNAME=root" -e "MONGO_INITDB_ROOT_PASSWORD=password" -e "MONGO_INITDB_DATABASE=Tarpaulin" mongo:latest
*/
/*
* 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 MongoDB server & the redis server.
*/
connectToDb(async function () {
redisClient.connect().then(function () {
app.listen(port, function () {
console.log("== Server is running on port", port)
})
})
})