node框架那些事儿
一、简单介绍
express:适合初学者,模版引擎,路由,中间件
koa2:核心中间件
eggjs:企业级应用框架
二、express
服务端框架,提供路由功能,异常处理。路由系统+中间件构成web开发框架,一个express应用就是调用各种中间件,express内部维护一个函数数组。中间件,在这里就是函数,不同的处理函数。
1、参考学习
express:http://www.expressjs.com.cn/
vscode调试express:https://www.cnblogs.com/camille666/p/vscode_debug_complex_nodejs_files.html
2、快速体验
# 快速搭建node应用程序 npm install express-generator -g express --view=ejs myapp
3、方法对比
app.use vs app.all vs express.Router().get
// express
const express = require('express');
const app = express();
app.use('/api/person' , function(req, res, next) {
res.send('respond with a resource');
});
// router
const express = require('express');
const router = express.Router();
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
app.use(path,callback)中的callback既可以是router对象又可以是函数,app.get(path,callback)中的callback只能是函数。
app.use('/a')只用路径以/a开始,可以匹配/a/b,/a/b/c;设置中间件,是否以什么开头;
app.all是具体的路由,直接使用字符串的方式,则匹配的是/a这个路径,不能匹配/a/b 、/a/b/c。设置路由,匹配完整路径。
express.Router是一个迷你版app。
app.listen vs http.createServer(app).listen
// express
const express = require('express');
const app = express();
app.listen('8085', () => {
console.log('人员管理系统');
});
// router
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
server.listen('8085');
server.on('error', function(err) {});
server.on('listening', function() {});
都可以创建http服务器,如果要创建https,需要引入https服务。
debug vs morgan
debug是一个通用的npm包,可以用在服务端,也可以用在浏览器端,用于调试日志,控制日志输出,不局限于请求日志,更不限于http请求;
morgan是express中间件,http请求日志中间件。
app.render vs res.render vs res.send
4、原理实践
/**
* 仿express实现中间件机制
*
* @return {app}
*/
function express() {
let funcs = []; // 待执行的函数数组
let app = function (req, res) {
let i = 0;
function next() {
const task = funcs[i++]; // 取出函数数组里的下一个函数
if (!task) { // 如果函数不存在,return
return;
}
task(req, res, next); // 否则,执行下一个函数
}
next();
}
/**
* use方法就是把函数添加到函数数组中
* @param task
*/
app.use = function (task) {
funcs.push(task);
}
return app; // 返回实例
}
// 测试
const http = require('http');
const app = express();
http.createServer(app).listen('3000', function () {
console.log('listening 3000....');
});
function middlewareA(req, res, next) {
console.log('middlewareA before next()');
next();
console.log('middlewareA after next()');
}
function middlewareB(req, res, next) {
console.log('middlewareB before next()');
next();
console.log('middlewareB after next()');
}
function middlewareC(req, res, next) {
console.log('middlewareC before next()');
next();
console.log('middlewareC after next()');
}
app.use(middlewareA);
app.use(middlewareB);
app.use(middlewareC);
node框架那些事儿的更多相关文章
- Java日志框架那些事儿
文章首发于[博客园-陈树义],点击跳转到原文Java日志框架那些事儿. 在项目开发过程中,我们可以通过 debug 查找问题.而在线上环境我们查找问题只能通过打印日志的方式查找问题.因此对于一个项目而 ...
- 从Nest到Nesk -- 模块化Node框架的实践
文: 达孚(沪江Web前端架构师) 本文原创,转至沪江技术 首先上一下项目地址(:>): Nest:https://github.com/nestjs/nest Nesk:https://git ...
- node框架koa
node的两大常见web服务器框架有express和koa,之前已经介绍过express了现在来介绍下koa吧~ koa也是express团队的出品,意在利用es7新出的async来告别"回 ...
- node框架express
见识到原生nodeJs服务器的恶心后,我们来用下简单好用的框架吧~ 服务器无非主要提供接口和静态文件读取,直接上代码: const express = require('express'); cons ...
- Node 框架接入 ELK 实践总结
本文由云+社区发表 作者:J2X 我们都有过上机器查日志的经历,当集群数量增多的时候,这种原始的操作带来的低效率不仅给我们定位现网问题带来极大的挑战,同时,我们也无法对我们服务框架的各项指标进行有效的 ...
- 《开源框架那些事儿22》:UI框架设计实战
UI是User Interface的缩写.通常被觉得是MVC中View的部分,作用是提供跟人机交互的可视化操作界面. MVC中Model提供内容给UI进行渲染,用户通过UI框架产生响应,一般而言会由控 ...
- AdonisJs(Node框架)学习总结
先列出作为前端开发人员必须知道的10个NodeJs框架 AdonisJs 一个服务端渲染的MVC框架,它是 Laravel (PHP框架)的一个 NodeJS 版本. 可以安装脚手架工具adonis ...
- node框架express里面静态文件中间件express.static,根据路径名查找文件
- 是express框架下的一个方法,可以根据请求路径名查找某个文件下文件名字和路径名相同的文件 - 3.X里面有20多个中间件,但是在4.X里面 只保留了express.static - 语法 ex ...
- express node 框架介绍
开篇先声明一个重点: 就是论文件模块的重要性,之前我一直以为 fs 模块不重要,后来遇到了问题,才发现我之前的自以为是是多么愚蠢的一件事,我现在知道了 fs 模块的重要性 fs 模块:用于对文件的操作 ...
随机推荐
- 使用 application.properties 中配置的属性,举例:@Value("${server.port}")
使用 application.properties 中配置的属性:@Value 注解. @RestController public class HelloWorldController { @Val ...
- 我的洛谷签名——Pale Blue Dot
We succeeded in taking that picture [from deep space], and, if you look at it, you see a dot. That's ...
- 【大数据】HBase环境
参考资料:https://www.cnblogs.com/frankdeng/p/9310191.html 主节点挂了 HBase服务访问:http://192.168.1.180:16010/mas ...
- Vsftpd 2.2.x安装和配置--centos7前的版本
Vsftpd 2.2.x安装和配置--centos7前的版本 原文链接:https://my.oschina.net/loubobooo/blog/1633367 1. 关闭防火墙和Selinux L ...
- SparkStreaming消费kafka中数据的方式
有两种:Direct直连方式.Receiver方式 1.Receiver方式: 使用kafka高层次的consumer API来实现,receiver从kafka中获取的数据都保存在spark exc ...
- Numpy | 01 简介
NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. NumPy 是一个运行速度非常快的数学库 ...
- PHP Record the number of login users
Function to record how many times the user logs in Connect to the database first: you can create a n ...
- 查看Linux机器的外网IP
curl icanhazip.comcurl ifconfig.mecurl curlmyip.comcurl ip.appspot.comcurl ipinfo.io/ipcurl ipecho.n ...
- pyqt(day3)
一.在pycharm中配置qtdesigner C:\Python\Python37\Lib\site-packages\pyqt5_tools\designer.exe 二.ui文件转换成pytho ...
- Note for Reidentification by Relative Distance Comparison
link Reidentification by Relative Distance Comparison Challenge: large visual appearance changes cau ...