gbk2utf.js 730 Bytes
'use strict';

const urlEncode = require('urlencode');
const _ = require('lodash');
const helpers = global.yoho.helpers;

module.exports = (req, res, next) => {
    let query = _.trim(req.query.query);
    let subDomain = req.subdomains[0];

    if (query) {
        try {
            decodeURIComponent(query);
            return next();
        } catch (e) {
            try {
                query = urlEncode.decode(query, 'gb2312');
                req.query.query = query;
                return res.status(301).redirect(helpers.urlFormat('', req.query, subDomain));
            } catch (e1) {
                return res.redirect(helpers.urlFormat('', null, subDomain));
            }
        }
    }

    return next();
};