-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
39 lines (30 loc) · 975 Bytes
/
app.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
"use strict";
const express = require("express");
const quotesRoutes = require("./quotesRoutes")
const nunjucks = require("nunjucks");
const app = express();
const cors = require('cors');
const { NotFoundError } = require("./expressError");
app.use(express.json());
app.use(cors());
app.use('/quotes', quotesRoutes);
nunjucks.configure('templates', {
autoescape: true,
express: app
});
/** Get homepage */
app.get("/", function (req, res, next) {
res.render("home.html")
})
/** 404 handler: matches unmatched routes; raises NotFoundError. */
app.use(function (req, res, next) {
return next(new NotFoundError());
});
/** Error handler: logs stacktrace and returns JSON error message. */
app.use(function (err, req, res, next) {
const status = err.status || 500;
const message = err.message;
if (process.env.NODE_ENV !== "test") console.error(status, err.stack);
return res.status(status).json({ error: { message, status } });
});
module.exports = app;