我知道Javascript,但對NodeJS很新。我想在我的代碼中的任何地方重新使用http請求,server.js之外。請看看:如何全局使用Node http請求
server.js
var http = require('http'); var mongodb = require('./db'); var settings = require('./settings'); var oauth = require('./oauth'); //start mongodb connection mongodb.connect(function(error){ if (error) console.error(error); else { //set db as mongodb client var db = mongodb.use(); //start http server http.createServer(function(request,response){ //set POST body request.body = ''; request.on('error',function(error){ console.error(error); response.statusCode = 404; response.end(); }).on('data',function(chunk){ request.body += chunk; }).on('end',function(){ //database and http server ready, start logic //Now I want to "require" a file here, such as test.js and call "request.headers" to work }); }).listen(settings.httpPort,function(){ console.log('Server listening on '+settings.httpServer+':'+settings.httpPort); }); } });
現在我想要求test.js裏面做以下的事情
test.js
console.log(request.headers);
注意:我不想使用任何像Express一樣的框架。我想從頭開始寫自己的東西。請幫助我,非常感謝你。
的可能的複製([node.js中做出的請求和響應全球對HTTP服務器?] http://stackoverflow.com/questions/7262727/node-js-make-request-and-response-global-for-http-server) – Abhijeet