nodejs之express中间件路由使用

1、express 中间件使用
/*
* 中间件:就是匹配路由之前和匹配路由之后做的一系列操作
*/

var express = require('express');
var app = new express();

/**
 * 内置中间件:托管静态页面
 */
//http://localhost:8001/news
app.use(express.static('public'));

//虚拟目录 http://localhost:8001/static/news
app.use('/static',express.static('public'));

 //ejs中 设置全局数据 所有的页面都可以使用

  app.locals['userinfo']='1213';

/**
 * 中间件:表示匹配任何路由
 * 应用级中间件
 * next() 路由继续向下匹配
 */
app.use(function (req,res,next) {
    console.log(new Date());
    next();
})

app.get('/',function (req,res) {
    res.send("hello express");
})

app.get('/news',function (req,res,next) {
    //res.send("hello news");
    console.log("news");
    next();
})

app.get('/news',function (req,res) {
    res.send("hello news1");
})

app.listen("8001");