比如 res.render = funcrion(view, data){ //TODO: }; 这个http模块的response从哪里获取?
这个http模块的response从哪里获取?
看文档:
https://nodejs.org/api/http.html https://nodejs.org/api/http.html#http_class_http_serverresponse
var res = { proto: http.ServerResponse.prototype }; exports.init = function(app){ return function expressInit(req, res, next){ if (app.enabled(‘x-powered-by’)) res.setHeader(‘X-Powered-By’, ‘Express’); req.res = res; res.req = req; req.next = next;
req.__proto__ = app.request;
res.__proto__ = app.response;
res.locals = res.locals || Object.create(null);
next();
}; }; express是利用中间件改写了req, res 看了一早上express源码 总算找到怎么弄了
var merge = require('merge-descriptors');
var http = require('http');
var EventEmitter = require('events').EventEmitter;
var res = {
__proto__: http.ServerResponse.prototype
};
res.render = function render(){
console.log('merge test is ok');
};
var app = {};
app.init = function init(){
merge(app, http, false);
merge(app, EventEmitter.prototype, false);
app.response = { __proto__: res, app: app };
//showFunctions(http);
return app;
};
app.defaultConfiguration = function defaultConfiguration(){
};
var showFunctions = function (object){
Object.getOwnPropertyNames(object).forEach(function forEachOwnPropertyName(name) {
console.log(name);
})
};
var test = app.init();
test.createServer(function(req, res){
res.__proto__ = app.response;
res.writeHead(200, 'text/html; charset=utf8');
res.render();
res.end('Server is ok');
}).listen(8888);
console.log('Server is running');
这个是我解决这个问题的test
http.ServerResponse.prototype 详见:http://godmod.cn/zupu/nodejs/NODEJS_MOD-NativeModule~~http