认识 http

通过 http.createServer(callback) 创建一个http服务,通过 listen(port, callback) 监听端口。

  1. const http = require('http')
  2. let app = http.createServer((req, res) => {
  3. console.log(req.url) // 打印 url
  4. res.write('hello world') // response写入
  5. res.end() // 结束写入,与 write 函数结合使用
  6. })
  7. app.listen(8080, () => {
  8. console.log('listen on 8080 port...')
  9. })

解析get请求

  • queryString 库的使用
  • url 库的使用

解析 post 请求

  • Buffer 的使用
  • app.on('data', callback)app.end(callback) 的使用

解决乱码问题