-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (43 loc) · 1.46 KB
/
server.js
File metadata and controls
49 lines (43 loc) · 1.46 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
const http = require('http')
const url = require('url')
const fs = require('fs')
const path = require('path')
/**
* this function is blocking, fix that
* @param {String} name full file name of asset in asset folder
*/
const findAsset = (name) => {
const assetPath = path.join(__dirname, 'assets', name)
return fs.readFileSync(assetPath, {encoding: 'utf-8'}).toString()
}
const hostname = '127.0.0.1'
const port = 3000
// log incoming request coming into the server. Helpful for debugging and tracking
const logRequest = (method, route, status) => console.log(method, route, status)
const server = http.createServer((req, res) => {
const method = req.method
const route = url.parse(req.url).pathname
// this is sloppy, especially with more assets, create a "router"
if (route === '/') {
res.writeHead(200, {'Content-Type': 'text/html'})
res.write(findAsset('index.html'))
logRequest(method, route, 200)
res.end()
} else {
// missing asset should not cause server crash
throw new Error('route not found')
res.end()
=======
// check the router for the incomming route + method pair
const routeMatch = router[`${route} ${method}`]
// return not found if the router does not have a match
if (!routeMatch) {
res.writeHead(404)
logRequest(method, route, 404)
return res.end()
}
// most important part, send down the asset
})
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`)
})