I am learning Node.js and making a webserver, and what I would like to do, is to require()
a file that executes nodejs code and capture that output of the file into a variable. is that possible?
I have the following:
Main.js
// Webserver code above
require('my_file.js');
// Webserver code below
my_file.js
console.log("Hello World");
I would like the output of Main.js to display Hello World
in a web browser, it does display in the console when I go to the url, but what is actually displaying on the page is console.log("Hello World");
Is there any way I can get the browser to display just the Hello World
and not the actual code?
Edit
When I do this:
http.createServer(function (request, response){
// Stripped Code
var child = require('child_process').fork(full_path, [], []);
child.stdout.on('data', function(data){
response.write(data);
});
// Stripped Code
}).listen(port, '162.243.218.214');
I get the following error:
child.stdout.on('data', function(data){
^
TypeError: Cannot call method 'on' of null
at /home/rnaddy/example.js:25:38
at fs.js:268:14
at Object.oncomplete (fs.js:107:15)
Am I not doing this correctly?