用 Node 原生 http 模块写一个服务,和用 Express/Koa 比有什么差异?

http 原生模块只提供底层请求/响应对象,路由、body 解析、中间件全要自己写:

import http from 'http'
const server = http.createServer((req, res) => {
  if (req.method === 'GET' && req.url === '/api') {
    res.writeHead(200, { 'Content-Type': 'application/json' })
    res.end(JSON.stringify({ ok: true }))
  } else {
    res.statusCode = 404
    res.end('Not Found')
  }
})
server.listen(3000)

对比框架:

选型:内部小工具可原生;业务服务用 Express/Koa/Fastify 提升效率,Fastify 在性能上更优。

同分类其他题目