How to run server written in js with Node.js


Question

I have installed node.js from here http://nodejs.org/ . in my windows8 machine. copied the example server code in my server.js file

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

then opened the node.js prompt and written node c:/node/server.js but nothing happens. I am a php developer just trying hands on it, any guidelines will really be helpful.

1
25
3/17/2017 11:43:24 PM

Accepted Answer

You don't need to go in node.js prompt, you just need to use standard command promt and write

node c:/node/server.js

this also works:

node c:\node\server.js

and then in your browser:

http://localhost:1337
30
1/6/2013 3:38:31 AM

Nodejs is a scripting language (like Python or Ruby, and unlike PHP or C++). To run your code, you need to enter a command in the terminal / shell / command prompt. Look for an application shortcut in your operating system by one of those names.

The command to run in the terminal will be

node server.js

But you will first need to browse in the terminal to the same folder as the file server.js. The syntax for using the terminal varies by operating system, look for its documentation.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon