learning express step(五)
learning express middleware
var express = require('express');
var app = express();
var log =
var myLogger = function (req, res, next) {
log++;
console.log('var log =', log);
next();
}; app.use(myLogger); app.get('/', function(req, res){
res.send("hello world");
}); app.listen();
result:
C:\Users\admin\WebstormProjects\learning-express-step5>node learning-express-step5.js
var log =
var log =
var log =
var log =
request time log:
var express = require('express');
var app = express();
var log =
var myLogger = function (req, res, next) {
log++;
console.log('var log =', log);
next();
}; var requestTime = function(req, res, next){
req.requestTime = Date.now();
next();
} app.use(myLogger);
app.use(requestTime); app.get('/', function(req, res){
//res.send("hello world");
var responseText = 'hello world!<br>';
responseText +='<small>Requested at: ' + req.requestTime + '</small>'
res.send(responseText);
});
result:
write middleware
var express = require('express');
var app = express();
var mw = require('./my-middleware'); var log = 0;
var myLogger = function (req, res, next) {
log++;
console.log('var log =', log);
next();
}; var requestTime = function(req, res, next){
req.requestTime = Date.now();
next();
} app.use(myLogger);
app.use(requestTime);
app.use(mw({option1:'1',option2:'2'})); app.get('/', function(req, res){
//res.send("hello world");
var responseText = 'hello world!<br>';
responseText +='<small>Requested at: ' + req.requestTime + '</small>'
res.send(responseText);
}); app.listen(3000);
my-middleware.js each request print log option1 and option2
module.exports = function (options) {
return function (req, res, next) {
console.log(options.option1);
console.log(options.option2);
next()
}
result:
C:\Users\admin\WebstormProjects\learning-express-step5>node learning-express-step5.js
var log = var log = var log =
learning express step(五)的更多相关文章
- learning express step(十四)
learning express error handle code: const express = require('express'); const app = express(); const ...
- learning express step(十三)
learning express error handle code: const express = require('express'); const app = express(); app.g ...
- learning express step(十二)
learning express view engine function const express = require('express'); const app = express(); app ...
- learning express step(十一)
learning express.Router() code: const express = require('express'); const app = express(); var route ...
- learning express step(四)
learning express route function const express = require('express'); const app = express(); app.get(' ...
- learning express step(九)
router-level middleware works in the same way as application-level middleware, except it is bound to ...
- learning express step(八)
To skip the rest of the middleware functions from a router middleware stack, call next('route') to p ...
- learning express step(七)
Route handlers enable you to define multiple routes for a path. The example below defines two routes ...
- learning express step(六)
code: use application middleware var express = require('express'); var app = express(); app.use(func ...
随机推荐
- 1204: 移位运算(C)
一.题目 http://acm.wust.edu.cn/problem.php?id=1204&soj=0 二.分析 无符号短整数关键字为:unsigned short: 无符号短整数长为2字 ...
- 【C#】课堂知识点#1
标准数字格式字符串 https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/standard-numeric-format-string ...
- Scratch教程:谁是真悟空
在西游记中,有一集是“真假悟空”,六耳猕猴变成了悟空的模样与真悟空真假难辨,打的不可开交. 在Scartch中,我们常常会使用一个本体来生成多个克隆体,这在开发过程中有重要的意义.但在实际操作中,每个 ...
- mybatis相关知识积累
mybatis Statement Statement对象用于将 SQL 语句发送到数据库中. 实际上有三种 Statement 对象,它们都作为在给定连接上执行 SQL语句的包容器: Stateme ...
- JS强制关闭浏览器页签并且不提示关闭信息
工作中很多奇葩的需求都会出现,现在就有一个问题,描述如下: 现在的登录跳转权限页面要去掉,集成在第三方系统信息上,当退出登录的时候需要关掉打开的Tab页面,因此考虑使用window.close()关闭 ...
- sqlite3 下载和安装步骤
1 下载地址 https://www.sqlite.org/2019/sqlite-tools-win32-x86-3300100.zip 2 添加系统变量 path中添加 sqlite3.exe所 ...
- Android Studio 代码页跳界面 /java和XML快速切换技巧
https://www.cnblogs.com/simadi/p/6698666.html?utm_source=itdadao&utm_medium=referral 今天又发现了一个And ...
- zblog2.X 连不上数据库原因
数据库配置Dim ZC_MSSQL_DATABASEZC_MSSQL_DATABASE="zb20" Dim ZC_MSSQL_USERNAMEZC_MSSQL_USERNAME= ...
- 二叉树BinaryTree构建测试(无序)
此测试仅用于二叉树基本的性质测试,不包含插入.删除测试(此类一般属于有序树基本操作). //二叉树树类 public class BinaryTree { public TreeNode root; ...
- wsl2 debian安装docker
应用商店下载debian 安装docker 安装依赖 打开安装好的docker安装依赖 sudo apt-get install apt-transport-https ca-certificates ...