path-decode.js 654 Bytes
/**
 * 处理伪静态化路由编码中间中间件
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2017/12/26
 */

const urlEncode = require('urlencode');
const _ = require('lodash');


module.exports = () => {
    return (req, res, next) => {
        let path = req.path;


        if (!req.xhr && _.indexOf(path, '%') > -1) {
            try {
                req.url = decodeURIComponent(path);
            } catch (e) {
                try {
                    req.url = urlEncode.decode(path, 'gb2312');
                } catch (e1) {
                    return next();
                }
            }
        }

        return next();
    };
};