I run a piece of code with first.js
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8124);
// Console will print the message
console.log('Server running at http://127.0.0.1:8124/');
while i m running it with gitbash its giving desire output. but with node js command prompt its giving microsoft jscript compilation error.
Looks like you've just typed script's name into the terminal window therefore asked windows to find proper interpreter for your script. You should try and interpret the script with nodejs instead:
node your-script.js
not just
your-script.js
The issue here could stem from several places. Most likely installation-based.
The following code (Taken from above)
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n'); }).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
Yields positive results when I attempt to load it in the browser. I therefore recommend the following:
1.1 If using any editor aside from a plain text editor (notepad++, notepad, sublime etc) ensure the file is in-fact set to output plain-text (bone-head move but often overlooked)
Reinstall node (Worth a shot)
If all else fails and you have npm installed, type "npm install http" and re-run your first.js application
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With