UNPKG

node-framework

Version:

node-framework

47 lines (37 loc) 1.19 kB
/** * @file init * @author sekiyika (px.pengxing@gmail.com) * @description * 初始化res和req */ var _ = require('lodash'); module.exports = function (app) { return function init(req, res, next) { app.emit('request:start', req, res); res.on('finish', function () { app.emit('request:end', req); }); // 添加接受请求的时间 req._startTime = new Date(); // generate pvid req.pvid = app.util.pvid(req._startTime); // 互相添加引用 req.res = res; res.req = req; // 传递到模板中的参数 res.locals = res.locals || {}; // 初始化设置_headers res._headers = res._headers || {}; // refer to https://github.com/ecomfe/edp-webserver/blob/master/lib/route.js#L100 var url = require('url'); // 在req中增加处理好的pathname,query等 _.extend(req, url.parse(req.url, true)); ['path', 'pathname', 'href'].forEach(function (key) { try { req[key] = decodeURIComponent(req[key]); } catch (ex) { } }); next(); }; };