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(五)的更多相关文章

  1. learning express step(十四)

    learning express error handle code: const express = require('express'); const app = express(); const ...

  2. learning express step(十三)

    learning express error handle code: const express = require('express'); const app = express(); app.g ...

  3. learning express step(十二)

    learning express view engine function const express = require('express'); const app = express(); app ...

  4. learning express step(十一)

    learning express.Router() code: const express = require('express'); const app = express(); var route ...

  5. learning express step(四)

    learning express route function const express = require('express'); const app = express(); app.get(' ...

  6. learning express step(九)

    router-level middleware works in the same way as application-level middleware, except it is bound to ...

  7. learning express step(八)

    To skip the rest of the middleware functions from a router middleware stack, call next('route') to p ...

  8. learning express step(七)

    Route handlers enable you to define multiple routes for a path. The example below defines two routes ...

  9. learning express step(六)

    code: use application middleware var express = require('express'); var app = express(); app.use(func ...

随机推荐

  1. TIM—基本定时器代码

    使用目的:使用TIM定时器让小灯每0.5秒翻转一次亮灭 编程过程: 1-配置时基初始化结构体 2-开启定时器更新中断(即定时时间到了) 3-配置中断优先级 4-使能定时器 5-编写中断服务函数 6-编 ...

  2. adb链接时报错误10061解决方法

    首先打开开发者选项然后依次操作: 1. 将安卓设备usb连接到电脑 2. 设备链接到wifi 3. Ping设备ip,检查是否可通信 4. 在cmd依次输入以下命令: 5. adb usb 6. ad ...

  3. C# DateTime Subtract

    DateTime start = DateTime.Now.AddDays(-20); DateTime end = DateTime.Now; TimeSpan ts = end.Subtract( ...

  4. (十二)Hibernate中的多表操作(2):单向多对一

    由“多”方可知“一”方的信息,比如多个员工使用同一栋公寓,员工可以知道公寓的信息,而公寓无法知道员工的信息. 案例一:使用xml配置 pojo类 Group.java package bean; // ...

  5. IdentityServer4密码模式接入现有用户数据表

    具体接入identityserver请看文档,这里只简单列举部分步骤 1.创建一个web项目,引入Identityserver4的nuget包 2.新建一个类,实现IResourceOwnerPass ...

  6. cli create ssl certkey

    cli create ssl certkey ############################### # 创建CA密钥 create ssl rsakey bwsrv-root.key -ex ...

  7. ipp 实现图像空间的转换

    下载:https://software.intel.com/en-us/parallel-studio-xe/choose-download/free-trial-cluster-windows-c- ...

  8. gcc和g++编译器

    .c文件,gcc当做c语言处理 .cpp文件,gcc和g++当做c++处理 .c文件和.cpp文件编译时,都是使用的gcc编译器 .cpp使用gcc链接,需要加入-lstdc++选项 举例 #incl ...

  9. vue拦截

    ```javascript import Vue from 'vue' import App from './App.vue' import router from './router' import ...

  10. Ceph 调整crush map

    目录 Ceph 调整crush map 1.前言 2.示例 1.创建新的replicated 2.修改当前pool的rule Ceph 调整crush map 1.前言 本文章适用于ceph n版 2 ...