Node.js是什么
Node.js 服务端开发的兴起
前端同学如何写Node.js 服务?
角色转变
章节
WEB服务端开发入门
简单的WEB 应用框架
前端 | 后端
client > HTTP < WEB Server
应用程序就是接受HTTP请求并返回 HTTP响应的函数
unction app(httpRequest) {
// application code
return httpResponse;
}
但现实中函数一般如下
function app(httpRequest, httpResponse) {
// application code
httpResponse.end()
}
HTTP over TCP/IP
浏览器浏览网址的过程
打开www.baidu.com URL
浏览器解析URL的IP DNS
HTTP请求 (GET HTTP/1.1 headers payload) TCP/IP
HTTP响应 (HTTP/1.1 200 OK headers Content-Length: 12 Hello world!)
使用TCP处理HTTP请求
github 源码地址
HTTP基于TCP
function app(socket: net.Socket) {
// application code
socket.end();
}
function app(httpRequest, httpResponse) {
// application code
httpResponse.end()
}
http echo server
Yap!Stream
import * as http from 'http'
http.createServer((request, response) => {
const { headers, method, url } = request;
response.writeHead(200, { 'Content-Type': 'application/json' })
response.write(JSON.stringify({ headers, method, url }, null, 2));
request.pipe(response);
}).listen(7500, function () {
console.log('opened server on', this.address());
})
tcp是基于字节流的 ,udp基于报文
小结
request headers 和body 如何获取?
1:headers 可以直接获取
2:body 需要自己解析(data,end event)
request 和 response 为什么分开?
HTTP(S) 网络协议进阶
request methods
HTTP状态吗
HTTP Headers
如何实现一个静态服务器?
MIME
上面有源码链接
import * as fs from 'fs'
import * as http from 'http'
import * as path from 'path'
内容协商
import * as http from 'http';
import * as json2html from 'node-json2html';
// 内容协商
如何控制缓存?
URl 后面加上时间戳就可以去掉缓存了,棒
缓存策略
Cache-Control
协商缓存
协商缓存时间为一天
import * as http from 'http';
import * as fs from 'fs';
import getMimeType from './get-mime-type';
小结
补充: 浏览器常见的网络协议
框架及架构模式介绍
我们面临的挑战
框架 + 合理的架构
Web服务器通用处理流程
Core J2EE Patterns - Intercepting Filter
public class DebuggingFilter implements Processor {
private Processor target;
public DebuggingFilter(Processor myTarget) {
target = myTarget;
}
public void execute(ServletRequest req,
ServletResponse res) throws IOException,
ServletException {
// preprocess
target.execute(req, res);
// post-process
}
}
Node.js 服务端框架
Express
var express = require('express');
var app = express();
app.get('/', function(req, res, next) {
next();
})
app.listen(7500);
express.Router
/*
文件 bird.js
*/
var express = require('express')
var router = express.Router()
// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
console.log('Time: ', Date.now())
next()
})
// define the home page route
router.get('/', function (req, res) {
res.send('Birds home page')
})
// define the about route
router.get('/about', function (req, res) {
res.send('About birds')
})
从链式到树的提升
chain of responsitility
Express 特点总结
优点
- 简单 框架级自带好用的路由,丰富的生态
缺点
- 回调 拦截处理不好实现
Koa.js
使用async functon, Node 7.6 及以上
异步中间件模型-洋葱模型
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
ctx.body = 'Hello World';
});
app.listen(7500);
异步中间件模型
中间件的执行顺序
Koa特点总结
优点
ThinkJS 3
企业级Web MVC 框架
ThinkJS 架构
ThinkJS 架构模式
MVC 架构模式
MVC 代码
// src/controller/user.js
module.exports = class extends think.Controller {
async indexAction() {
const users = await this.model('user').getList(); // 调用 model
this.assign('users',users); //给模板赋值
return this.display(); //渲染模板
}
}
// src/model/user.js
module.exports = class extends think.Model {
getList() {
return this.field('name').select();
}
}
///view/user.html
Hello #{users[0].name}!
IoC 依赖反转
场景:
目标
IoC模式实现
ThinkJS extend adapter
简单对比
框架 | Express | Koa | ThinkJS |
---|---|---|---|
Router | ✓ |
✓ |
|
Promise | ✓ |
✓ |
|
Templating | ✓ |
✓ |
|
Enterprise | ✓ |
✓ |
|
MVC | ✓ |
||
IoC | ✓ |
||
TypeScript | ✓ |
✓ |
✓ |
多模块 | ✓ |
✓ |
我们要不要用企业级别框架?
小结
介绍对比了三个框架
介绍几个架构模式