-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (31 loc) · 1.17 KB
/
index.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
//Main code file
//Created by Callum Gilchrist
// Require needed library's for express
const express = require(`express`);
const ejs = require(`ejs`);
const cookieParser = require(`cookie-parser`);
const dotenv = require(`dotenv`).config();
//Import homemade modules
const routes = require(`./routes`);
//Create app object and set a "port" variable
const app = express();
const port = 3030;
//Allow the /public directory to be accessed by anyone
app.use(`/public`, express.static(`public`));
//Use EJS as the view engine to allow for template use
app.set(`view engine`, `ejs`);
//Allow the user to parse data to the sever through urlencoded and JSON
app.use(express.urlencoded({extended: true}));
app.use(express.json());
//Allow the sever to parse cookies and set the secret
app.use(cookieParser(process.env.COOKIE_SECRET));
//Forward requests made to / to the routes module
app.use(`/`, routes);
//Handle 404 page not found
app.all(`*`, (req,res) => {
res.render(`errorpage`, {errorList: {errorCode: 404, Description: "Page not found"}});
})
//Listen on the port defined at the top of the program
app.listen(port, () => {
console.log(`App listening on http://localhost:${port}`)
})