zoukankan      html  css  js  c++  java
  • 2.中间件的使用

                                                                                                      中间件的使用

      新建node 文件夹, npm init -y快速创建package.json文件。npm install express --save 安装express. 创建index.js 文件如下

      let express = require("express"); // 引入express

    let app = express(); // connect() 创建服务器。

    // logger中件间,有next参数

    let logger =function(req,res,next){

        console.log(req.url);

        next();

    }

    // hello 中间件,没有next 参数。

    let hello = function(req,res){

        res.writeHeader(200, {"content-type": "text/html"});

        res.end("hello world");

    }

    // use使用中间件

    app.use(logger);

    app.use(hello);

    app.listen(8080)

     这时调用cmd命令窗口,输入node index 启动服务器,浏览器中输入localhost:8080, 可以看到hello world,同时控制台中输出 /. 我们用中间件的方式重写了hello world 程序。

  • 相关阅读:
    networktool3
    networktool2
    networktool
    Intel Zepher 介绍
    使用IPMI发送事件1让BMC log 填满
    Knights Landing
    Intel历代处理器
    Intel Datacenter Group Public Roadmap
    django的url路由
    position用法
  • 原文地址:https://www.cnblogs.com/Lin461179097/p/10264249.html
Copyright © 2011-2022 走看看