建立一個最基本的 http-server

沒有那麼複雜。

原始碼

備註:res.end() 的意思不是結束。是送出 response 的意思。

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
// 引入 http 模組
const http = require('http')
// 伺服器的相關設定
const server = http.createServer((req, res) => {
// 當存取 '/' 時,回傳 welcome
if(req.url === '/') {
res.write('Welcome')
res.end()
return
}
// 當存取 '/hello' 時,回傳 hello
if(req.url === '/hello') {
res.write('hello')
res.end()
return
}
// 當存取 '/redirect' 時,回傳 302 狀態碼,以及要導向的網址
if(req.url === '/redirect') {
res.writeHead(302, {
'Location': 'https://google.com/'
})
res.end()
return
}
// 當存取不存在的資源 時,回傳 404 狀態碼
res.writeHead(404)
res.end()
})
server.listen(5000) // 提供 xxx 服務的意思(NBA 即時賽況)
SDK 與 API 之間的關係 curl 的使用方法
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×