Mícéal Gallagher in Nodejs 5 minutes

Node.js - Simple server example

Node.js is a platform that allows for the creation of server-side applications written in JavaScript. In this example we will create a basic server that will listen on port 8080, take the value of a parameter called ‘name’ from the query string and print ‘Hello, **'. If you are running Ubuntu, you can install Node.js by running the following command from a terminal:

```bashsudo apt-get install nodejs


Now open your favourite text editor and let's start adding some code. We need to use a few modules, and to add them to your application your need to use the `require` function. For this example we need three modules; `http`, `url` and `querystring`.

```javascriptvar http = require('http');
var url = require('url');
var queryString = require('querystring');

Let’s create our server. Call createServer and specify a function that accepts two parameters - one to represent the client request and the other to represent the response. At this point, most developers typically create an inline anonymous function which I’m just not a big fan of doing - I prefer to have my functions defined by a name.


We need to create the function serverRequester, but first we must create a function that will take a request and parameter name. If it is found within the request query string, then return the value of that parameter.

```javascriptfunction getRequestParameterValue(request, parameterName) { // Prase the URL string to get a URL object var parsedUrl = url.parse(request.url); // Get a queryString object by parsing the query string of the URL var queryParameters = queryString.parse(parsedUrl.query); // Return the value of the paramter ‘name’ return queryParameters[‘name’]; }


Notice the use of `queryString.parse();` this call will parse a URL query string and return a collection. A query string such as `?userId=6&userName=Miceal` will be returned as `{userId:6,userName:Miceal}`. The values of the returned collection can be accessed by using the parameter names as keys like so

```javascriptqueryParameters['name'];

Finally we create the serverRequester function that we used as part of createServer. All this function will do is call getRequestParameterValue and display the value if one exists.

```javascriptfunction serverRequester(request, response) { console.log(‘Incomming request’); // Attempt to get the value of the parameter ‘name’ var name = getRequestParameterValue(request, ‘name’); if ( name ) { console.log(‘Name parameter has been successfully parsed from request’); response.writeHead(200); // Output the name of the person response.write(‘

Hello, ‘ + name + ‘

’); } else { console.log(‘Malformed request. No name parameter’); response.writeHead(400); } response.end(); }


Now lets run the code. Open up a terminal and navigate to the directory where the code file is located,  and then run the command

```bashnodejs ExampleCode.js

Now our server is running and listening on port 8080, we can open a browser and navigate to http://localhost/Mehaul:8080/?name=Miceal or we can run the following command:

```bashcurl http://localhost/Mehaul:8080/?name=Miceal Outputs:

Hello, Miceal

;

```

Get the code on GitHub References: Http Node.js Doc URL Node.js Doc QueryString Node.js Doc