Koa.js 是基于中间件模式的HTTP服务框架,底层原理是离不开Node.js的http
原生模块。
const http = require('http');const PORT = 3001;const router = (req, res) => {res.end(`this page url = ${req.url}`);}const server = http.createServer(router)server.listen(PORT, function() {console.log(`the server is started at port ${PORT}`)})
这里的服务容器,是整个HTTP服务的基石,跟apache
和nginx
提供的能力是一致的。
const http = require('http');const PORT = 3001;const server = http.createServer((req, res) => {// TODO 容器内容// TODO 服务回调内容})server.listen(PORT, function() {console.log(`the server is started at port ${PORT}`)})
服务回调,可以理解成服务内容,主要提供服务的功能。
req
res
const router = (req, res) => {res.end(`this page url = ${req.url}`);}
是服务回调中的第一个参数,主要是提供了HTTP请求request
的内容和操作内容的方法。
更多操作建议查看 Node.js官方文档
https://nodejs.org/dist/latest-v8.x/docs/api/http.html
https://nodejs.org/dist/latest-v10.x/docs/api/http.html
是服务回调中的第二个参数,主要是提供了HTTP响应response
的内容和操作内容的方法。
注意:如果请求结束,一定要执行响应 res.end()
,要不然请求会一直等待阻塞,直至连接断掉页面崩溃。
更多操作建议查看 Node.js官方文档
https://nodejs.org/dist/latest-v8.x/docs/api/http.html
https://nodejs.org/dist/latest-v10.x/docs/api/http.html
通过以上的描述,主要HTTP服务内容是在 “服务回调
” 中处理的,那我们来根据不同连接拆分一下,就形成了路由router
,根据路由内容的拆分,就形成了控制器 controller
。参考代码如下。
const http = require('http');const PORT = 3001;// 控制器const controller = {index(req, res) {res.end('This is index page')},home(req, res) {res.end('This is home page')},_404(req, res) {res.end('404 Not Found')}}// 路由器const router = (req, res) => {if( req.url === '/' ) {controller.index(req, res)} else if( req.url.startsWith('/home') ) {controller.home(req, res)} else {controller._404(req, res)}}// 服务const server = http.createServer(router)server.listen(PORT, function() {console.log(`the server is started at port ${PORT}`)})