1.看书的时候在express书上发现这段代码很有意思:
app.param(['id', 'page'], function (req, res, next, value) { console.log('CALLED ONLY ONCE with', value); next(); }) app.get('/user/:id/:page', function (req, res, next) { console.log('although this matches'); next(); }); app.get('/user/:id/:page', function (req, res) { console.log('and this matches too'); res.end(); });
当我们运行 /user/42/3的时候,会发现它输出如下结果
分析起来很有意思,看结果可以知道,当运行 /user/42/3的时候,应该先找到 第一个/user/:id/:page符合,然后通过next()方法运行下一个,直到res.end()才结束但是结果会先输出第一个,可以看出,他是直接在req.param()中直接 id=42,page=3;CALLED ONLY ONCE with 42 CALLED ONLY ONCE with 3 although this matches and this matches too
2.url的部分参数
app.use('/admin', function(req, res, next) {
// GET 'http://www.example.com/admin/new'
console.log(req.originalUrl); // '/admin/new'
console.log(req.baseUrl); // '/admin'
console.log(req.path); // '/new'
next();
});
3.express中间件的存放又一定的位置,一定要放在request请求之后,否则永远无法执行,我个人觉得通过下面的app.use()方法,可以测试每个路由请求的时间消耗比。
app.use(function (req, res, next) {
console.log('Time: %d', Date.now());
next();
})
4.我有一个想法,就是能不能把定时任务直接交给子线程,等结束后将结果返回主线程,在关闭子线程。但我还没有整明白在nodejs中怎么调用,悲剧呀。
5.这几天操作linux系统,每次学习都发现vi用不好,这次非得花点时间去整明白咋么使用:
vi test.txt //不管test.txt是否存在
按下i进入编辑模式,开始编辑文字
按下ESC键回到一般模式
在一般模式中输入“:wq” 保存后离开vi
至此vi的一般用法就结束了。
6.还需要继续详细vi及其它命令。