-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (35 loc) · 1.05 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
41
42
43
44
45
const http = require('http');
const fs = require('fs')
const hostname = '127.0.0.1';
const port = process.env.PORT || 5000;
const server = http.createServer((req, res) =>{
//Set up dynamic path
let path = './userStories/';
if(req.url === '/' || req.url === '/home'){
path = path + 'home.html'
}else if(req.url === '/contact'){
path = path + 'contact.html'
}else if(req.url === '/about' || req.url === '/about-us'){
path = path + 'about.html'
}
else {
path = path + 'error.html';
}
// Read files from userStories
fs.readFile(path, (err, data)=>{
//set response header
if(path === './userStories/error.html'){
res.writeHead(404, {'content-type':'text/html'})
}else{
res.writeHead(200, {'content-type':'text/html'})
}
if(err){
console.log(err)
return;
}
res.end(data)
})
})
server.listen(port, hostname, () =>{
console.log(`Server listening on http://${hostname}:${port}`)
})