-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
54 lines (46 loc) · 1.63 KB
/
app.js
File metadata and controls
54 lines (46 loc) · 1.63 KB
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
const express = require('express') // Import Express.js module
// Import routers
const getRouter = require('./router/get')
const postRouter = require('./router/post')
// Import middleware
const { urlencoded } = require('body-parser')
const cookieParser = require('cookie-parser')
const consolidate = require('consolidate')
const app = express() // Create Express.js application
app.use(express.static(__dirname + "/public")) // Serve static files in public directory
app.use(urlencoded({ extended: true })) // Parse request bodies
app.use(cookieParser()) // Parse cookies
// Use Mustache as HTML rendering engine
app.engine('html', consolidate.mustache)
app.set('views', __dirname + "/public")
app.set('view engine', 'html')
// Enable routers for GET/POST requests
app.use("/api", postRouter)
app.use("/", getRouter)
// 404 error page
app.use((req, res, next) => {
res.end(`
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
<title>Error 404</title>
</head>
<body>
<div class="home">
<h1 class="home__header">Error 404</h1>
<p>The page you are looking for may have:</p>
<ul>
<li>Been deleted</li>
<li>Been moved</li>
<li>Never existed</li>
<li>Be a typo</li>
</ul>
<a href="/">Click Here to Go Home</a>
</div>
</body>
</html>
`)
})
module.exports = app