Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run server written in js with Node.js

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.

like image 706
Rupesh Patel Avatar asked Jan 04 '13 09:01

Rupesh Patel


People also ask

CAN node js be used as a server?

Node. js provides capabilities to create your own web server which will handle HTTP requests asynchronously. You can use IIS or Apache to run Node. js web application but it is recommended to use Node.


2 Answers

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 
like image 170
Peter Krejci Avatar answered Sep 20 '22 13:09

Peter Krejci


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.

like image 40
Colonel Panic Avatar answered Sep 24 '22 13:09

Colonel Panic