-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
81 lines (80 loc) · 2.45 KB
/
server.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// build out a server that renders plain text
const http = require('http')
const server = http.createServer((req, res) => {
console.log('someone connected')
res.writeHead(200, {'Content-Type': 'text/plain'})
res.write('You have reached server 1337')
res.end()
})
server.listen(1337)
// build out a server that renders json
const server2 = http.createServer((req, res) => {
console.log('someone connected to server 2')
res.writeHead(200, {'Content-Type': 'application/json'})
let myObj = {message: 'how you like server bro?', status: 200, mimeType: 'json'}
res.write(JSON.stringify(myObj))
res.end()
})
server2.listen(1738)
// build out a server that renders html
const codeIm = require('./codeimmersive')
const server3 = http.createServer((req, res) => {
console.log('connected to server 3 something else')
res.writeHead(200, {'Content-Type': 'text/html'})
res.write(codeIm)
res.end()
})
server3.listen(2222)
// check the request object for the url
const fs = require('fs')
const nfl = http.createServer((req, res) => {
let url = req.url
let queryString = ''
if (url.indexOf('?') >= 0) {
queryString = url.substring(url.indexOf('?') + 1, url.length)
// key=value&key2=value2
}
console.log(req.url, queryString)
if (url === '/') {
let index = fs.readFileSync('index.html', 'utf-8')
res.writeHead(200, {'Content-Type': 'text/html'})
res.write(index)
res.end()
} else {
let pathName = url.substr(1, url.length)
fs.readFile(`${pathName}.html`, 'utf-8', (err, contents) => {
if (err) {
let notFound = fs.readFileSync(`404.html`, 'utf-8')
res.writeHead(404, {'Content-Type': 'text/html'})
res.write(notFound)
res.end()
} else {
res.writeHead(200, {'Content-Type': 'text/html'})
res.write(contents)
res.end()
}
})
}
// switch (url) {
// case '/':
// let index = fs.readFileSync('index.html', 'utf-8')
// res.write(index)
// break
// case '/raiders':
// pathName = url.substr(1, url.length)
// let raiders = fs.readFileSync(`${pathName}.html`, 'utf-8')
// res.write(raiders)
// break
// case '/cowboys':
// pathName = url.substr(1, url.length)
// let cowboys = fs.readFileSync(`${pathName}.html`, 'utf-8')
// res.write(cowboys)
// break
// default:
//
// }
})
nfl.listen(3000)
// use 3rd party node modules
// based on url render different html files
// maybe get into params