zoukankan      html  css  js  c++  java
  • express 中间件

    1.compression

    github:https://github.com/expressjs/compression

    开启 gzip 压缩对你的 Web 应用会产生巨大影响。当一个 gzip 压缩浏览器请求某些资源的时候,服务器会在响应返回给浏览器之前进行压缩。如果你不用 gzip 压缩你的静态资源,浏览器拿到它们可能会花费更长时间。

    在 Express 应用中,我们可以用内建 express.static() 中间件来处理静态内容。此外,还可以用compression中间件压缩和处理静态内容。

    var compression = require('compression');
    app.use(compression());

    // 要在 express.static() 之前使用 app.use(express.static(path.join(__dirname, 'public')));

     

    2.csurf

    github:https://github.com/expressjs/csurf

    csurf是为了防止 跨站请求伪造的

    使用csurf模块

    var csurf = require('csurf');   //引入模块

    var csrfProtection = csurf();   //设置路由中间件

    router.use(csrfProtection); 

    router.get('/user/signup',function(req,res,next){

      res.render('user/signup',{

         csrfToken: req.csrfToken()

      });

    });

    隐藏csrfToken

    <input type="hidden" class="form-control" name="_csrf" value="{{csrfToken}}">

    关于跨域请求伪造请看这篇文章:http://www.cnblogs.com/y-yxh/p/5761941.html

    4.helmet

    github:https://github.com/helmetjs/helmet

    Helmet是帮助增强Node.JS之Express/Connect等Javascript Web应用安全的中间件。

    一些著名的对Web攻击有XSS跨站脚本, 脚本注入 clickjacking 以及各种非安全的请求等对Node.js的Web应用构成各种威胁,使用Helmet能帮助你的应用避免这些攻击。

    Helmet安全功能有:

    1. csp: 通过设置Content-Security-Policy来阻止XSS攻击和一些其他的跨站注入风险。
    2. hidePoweredBy: 删除了header中的X-Powered-By标签
    3. hpkp:通过增加Public key Pinning来阻止伪造证书导致的中间人攻击。
    4. hsts: 设置 Strict-Transport-Security 来强制安全连接(http over SSL)到服务器(服务器需要支持https)
    5. ieNoOpen:为ie8设置 X-Download-Options
    6. noCache:设置Cache-Control关闭客户端缓存。
    7. noSniff: 设置X-Content-Type-Options阻止浏览器MIME-sniffing。
    8. frameguard:设置X-Frame-Options阻止点击劫持风险
    9. xssFilter: 设置X-XSS-Protection启用XSS过滤器

     

    var helmet = require('helmet');
    app.use(helmet());
     

    5.express-session

      github: https://github.com/expressjs/session

      众所周知,HTTP 是一个无状态协议,所以客户端每次发出请求时,下一次请求无法得知上一次请求所包含的状态数据。难道每打开个页面都要登录一次吗?

      所以cookie产生来解决这个问题。

      cookie 虽然很方便,但是使用 cookie 有一个很大的弊端,cookie 中的所有数据在客户端就可以被修改,数据非常容易被伪造,那么一些重要的数据就不能存放在 cookie 中了,而且如果 cookie 中数据字段太多会影响传输效率。为了解决这些问题,就产生了 session,session 中的数据是保留在服务器端的。

    这篇文章说的很详细:http://wiki.jikexueyuan.com/project/node-lessons/cookie-session.html

    var session = require('express-session');
    var RedisStore = require('connect-redis')(session);
    app.use(session({
      name: 'widgetdiy',
      secret: config.session.secret,
      cookie: {
        maxAge: config.session.maxAge,
        httpOnly: true,

        // 只有在https中有效
        // secure: true
      },
      // 每次请求都重新设置session的有效时间
      resave: true,
      saveUninitialized: true,
      store: new RedisStore(config.redis)
    }));

    // 6.passport

    // 7.logger

  • 相关阅读:
    Swagger2 添加HTTP head参数
    获取枚举类型描述
    JS设置cookie、读取cookie、删除cookie
    ES6中Promise的入门(结合例子)
    阮一峰的ES6---Promise对象
    model_util.py
    IfcSpatialElementType
    labelme coco
    python opencv KeyPoint
    IfcSpatialZoneType
  • 原文地址:https://www.cnblogs.com/sungg/p/6808155.html
Copyright © 2011-2022 走看看