美文网首页
nodejs express中间件

nodejs express中间件

作者: super静_jingjing | 来源:发表于2019-01-29 18:57 被阅读0次

Express 是一个自身功能极简,完全是由路由和中间件构成一个的 web 开发框架:从本质上来说,一个 Express 应用就是在调用各种中间件。
中间件(Middleware) 是一个函数,它可以访问请求对象(request object (req)), 响应对象(response object (res)), 和 web 应用中处理请求-响应循环流程中的中间件,一般 被命名为 next 的变量。

中间件的功能包括 :
-->执行任何代码
-->修改请求和响应对象
--> 终结请求-响应循环
-->调用堆栈中的下一个中间件。

Express 应用可使用如下几种中间件:
--> 应用级中间件

const express = require("express");
const app = new express();
//中间件 -- 表示匹配任何路由 -- 应用级中间件
//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){
    res.send("新闻页面");
});
app.listen("3001","127.0.0.1");

-->路由级中间件

const express = require("express");
const app = new express();
app.get("/",function(req,res){
    res.send("hello express");
});
app.get("/news",function(req,res,next){
    console.log("路由中间件");
    //继续向下匹配路由
    next();
});
app.get("/news",function(req,res){
    res.send("这是路由中间件---新闻页面");
});
app.listen("3001","127.0.0.1");

-->错误处理中间件

const express = require("express");
const app = new express();
app.get("/",function(req,res){
    res.send("hello express");
});
app.get("/news",function(req,res){
    res.send("这是路由中间件---新闻页面");
});
//匹配所有的路由 404  路由找不到的时候会匹配这个路由
app.use(function(req,res){
    res.status(404).send("这是404,表示路由没有匹配到");
});
app.listen("3001","127.0.0.1");

-->内置中间件

const express = require("express");
const app = new express();
//内置中间件  托管静态文件--可以直接http://localhost:3001/css/head.css访问到css
app.use(express.static("public"));
//虚拟出/static路径代替public
// app.use("/static",express.static("public"));
app.get("/",function(req,res){
    res.send("hello express");
});
app.get("/news",function(req,res){
    res.send("这是路由中间件---新闻页面");
});
//
app.listen("3001","127.0.0.1");

-->第三方中间件

const express = require("express");
const bodyParse = require("body-parser");
const app = new express();
app.set("view engine","ejs");
//cookie  session 获取post提交的数据
//通过第三方中间件获取post提交的数据
//第三方中间件 body-parse
app.use(bodyParse.urlencoded({extended:false}));
app.use(bodyParse.json());
app.get("/",function(req,res){
    res.send("hello express");
});
app.get("/login",function(req,res){
    res.render("login");
});
app.post("/doLogin",function(req,res){
    //获取【post提交的数据
    console.log(req.body);
});
app.listen("3001","127.0.0.1");

相关文章

  • 正在学习的项目

    nodejs+mongodb+ejs+express 学到的知识 mongodb的常用操作express中间件的使...

  • nodejs之koa中间件源码解析

    前言 上一篇《nodejs之express中间件》已经对express中间件的实现做了详细的讲解,同时也对实现中间...

  • Nodejs异步回调之异常处理实例

    目前我们项目的Nodejs异常是通过express next 到 errorhandler 中间件去处理的,原本以...

  • Nodejs异步回调之异常处理

    目前我们项目的Nodejs异常是通过express next 到 errorhandler 中间件去处理的,原本以...

  • nodejs express中间件

    Express 是一个自身功能极简,完全是由路由和中间件构成一个的 web 开发框架:从本质上来说,一个 Expr...

  • Nodejs express中间件

    express中间件可以理解为由含有req,res,next三个参数的方法,通过next()无限调用 基本样式(r...

  • 3.模块

    1.multer multer用于处理文件上传的nodejs中间件,主要跟express框架搭配使用,只支持表单M...

  • Express中间件

    Express框架是由路由和中间件构成的一个web开发框架。 1. Express 中间件 中间件是Express...

  • nodejs express-jwt解析

    作用是什么 express-jwt是nodejs的一个中间件,他来验证指定http请求的JsonWebTokens...

  • Nodejs(Express) - 04 中间件

    一、什么是中间件 中间件(Middleware)是在http的请求和响应之间进行拦截处理的模块, express通...

网友评论

      本文标题:nodejs express中间件

      本文链接:https://www.haomeiwen.com/subject/duffsqtx.html