-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathweb.js
More file actions
34 lines (23 loc) · 794 Bytes
/
web.js
File metadata and controls
34 lines (23 loc) · 794 Bytes
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
const express = require('express');
const { readFile } = require('fs');
const path = require('path')
const fs = require('fs').promises
const port = 3002;
const server = express();
server.use(express.static('public'));
const HomepagePath = path.join(__dirname, 'public', 'index.html')
const NotFoundPagePath = path.join(__dirname, 'public', '404.hmtl')
const handleHomepage = async (req, res) => {
const file = await fs.readFile(HomepagePath)
res.status(200).sendFile(file)
}
server.get('/index.html', handleHomepage)
server.get('*', async (req, res) => {
try {
const file = await fs.readFile(NotFoundPagePath)
res.sendFile(file)
} catch(error) {
console.log(error)
}
})
server.listen(port, () => console.log(`listening on port: ${port}`))