How to get base url in nodejs

Example URL : http://localhost:9000/api/users

Expected information : localhost:9000

Code to get this information

// req is the request object coming in via web server i.e. Express.

var host=req.get(‘host’); //  this will give “localhost:9000”

 

Advertisements

Getting started with NodeJs Part-2 : Write simple web server in NodeJs

we have seen the setup of Nodejs on windows box in  last post.

Today we will have a look how we can write a simple web server which respond “Hello World” to our request

Steps

1. Open your favorite editor (i.e Notepad)  I personally like “Sublime Text 2”

2. copy the code below and save your file with “HelloWorld_Server.js”

/*

Author : Dilip Singh

HomePage : D4dilip.Wordpress.com

Sample : Hello world sample in NodeJs

Description : Simple hello world example

To test the sample , type into browser address bar ‘http://localhost:8080’

You should see the response ‘Hello World’

*/

 

// htp module required to create http server

var http = require(‘http’);

var server = http.createServer();

 

//this function will handle all the requests to this server

function handleMyAllRequests(req,resp)

{

// create here message

resp.writeHead(200, {“content-type”:”text/plain”});

resp.write(“Hello World”);

resp.end();// you can also write more string in the construct of end() function.

 }

 

// on every request event handleMyAllRequests function will be callled

server.on(‘request’, handleMyAllRequests);

 

// assign a port to the server

server.listen(8080,’127.0.0.1′);

console.log(‘server is listening at http://127.0.0.1:8080’);

3. Now go to command prompt and type :

c:\><directory of your file> > Node HelloWorld_Server.js

4. Press Enter .You should see the server log that its started.

5.  Go to web browser and type address “http://localhost:8080&#8221;

you will see the response from your first nodejs server

Code Explanation

In NodeJs, we need to import the ‘module’ for type of work we want to perform . In the example above we have imported module ‘http’. A module in

imported using require() function.

You can also optimise your code above 

// http module required to create http server

require(‘http’).createServer().on(‘request’, function handleMyAllRequests(req,resp){

// create here message

resp.writeHead(200, {“content-type”:”text/plain”});

resp.end(“Hello World with optimised code “);

} ).listen(8080);

 

Getting started with NodeJs Part-1 : Setup NodeJs Windows on box

NodeJS  or Node.js is relatively new technology ( from 2009).

what is Node.js?

 Node is a server-side JavaScript interpreter that changes the notion of how a server should work. Its goal is to enable a programmer to build highly-scalable applications and write code that handles tens of thousands of simultaneous connections on one, and only one, physical machine.

Source :http://www.ibm.com/developerworks/opensource/library/os-nodejs/index.html

Node  is very helpful to build scalable network programs.

Setup steps :

1. Download msi file from http://nodejs.org/download/

2. Once installation is finished run command

c:\>node -v

you should see the current installed version

Now you are good to  go with NodeJs