node.js 入门教程之五 -- HTTP request and response
搭建 http server
下面是一个 http server 的示例:
const http = require('http')
process.env.PORT = 3000
const port = process.env.PORT
const server = http.createServer((req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/html')
res.end('<h1>hello world</h1>')
})
server.listen(port, () => console.log(`server running at port ${port}`))
访问 http://localhost:3000 就会看到页面显示 hello world。