I know Javascript but very new to NodeJS. I want to reuse http request anywhere in my code, outside of server.js. Please take a look:
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);
});
}
});
And now I want to require test.js which do the following thing
test.js
console.log(request.headers);
Note: I don't want to use any framework like Express. I want to write my own things from scratch. Please help me, thank you so much.