Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

The Node.js -- Part7: Advanced HTTP Server-1

node node.js http server flatiron

  • Please log in to reply
2 replies to this topic

#1 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 07 March 2013 - 02:40 AM

We had come to know about basic http server that was able to server files requested by the client. Today we will start learning how to make an advanced http server which is able to handle various types of requests from client side. In this part of this advanced server we will start designing an application that has single html page with some links. Our http server will handle all the requests from that page. Thus we will complete our advanced server.

 

The Goal

Our goal is to create the final http server application that can handle all http requests from client side. We will use some modules/platforms like flatiron for this purpose. Our application will have some html pages with lots of links/requests, forms etc. We will create an html page home.html. We will send this page on client request and we will use this as our application starter page.
 
Basic HTTP Server
Our basic http server that we created earlier: 
var http = require('http');
var server = http.createServer(function(req,res){
    //request handler code here
    console.log('Request found.');
    res.end('Hello world');
});
server.listen('8080');
console.log('Server is running.');
This was our basic http server that can send a message on client request. If we want to process requests from client side we need to parse the url got and then need to make the handler for some requests. Here we will implement an easy solution for all requests part by part.
 

 

The Application using flatiron

We will use a node module named flatiron which is used for various purpose. Flatiron is a platform that run on node, we can tell flatiron as node framework.  Flatiron gives us a commandline tool that can generate several application boilerplates. Installing flatiron is easy. Just run this in node commandline
npm install flatiron -g
This command will install flatiron framework glabally that means you can use this in anywhere in all your applications.  Flatiron gives us a good commandline tool and we can use this by ‘flatiron’ command in node commandline. Now we will create our application by this command
flatiron create httpserver
There will be some prompt that will ask for some initial information about the author description of the app etc. After providing all those flatiron  will create our application in the httpserver(name of the app) folder in current working directory with some files and by default on http://localhost:3000. Directory structure of the newly created application:
createserver.png
Our application starter file is app.js, package.json contains the information about the application, config folder has a config/config.json file that can be edited to configure our application with some default parameters/values, the lib and test initially don't have any file. 
Well now we are set to run our application by the command, reaching in the httpserver directory:
node app.js
 
This will show an error like node module not found with some helpful messages, we have to install all the dependencies for this application by this command
npm install
This command will install all the dependencies into our application httpserver folder. Now we are set to see what we created. After running the app, when we hit http://localhost:3000 we will see a message {"hello":"world"} in the browser. The request handler looks like:

app.router.get('/', function () {
  this.res.json({ 'hello': 'world' });//sending  json data
});

From now on we will work in the app.js file. Now we set the home.html for our starter application page.

app.router.get('/', function () {//default request handler, for start point of our app, finally here will be login form
var data = fs.readFileSync('./html/home.html');

console.log('Sending read file: home.html');
this.res.end(data);
});
 
The app object is the current application object is used to do everything in server purpose. The router object is used to route a request path to some specified callback function.
See we placed the html file in html folder in the httpserver. We read the file on http://localhost:3000('/') request and its readFileSync() that reads file synchronously. Then the data is sent back to the client side. Thus we initialize our application. Now its easy to make some other request handlers(append this after the previous block of code) 

app.router.get('about', function () {
    var data = fs.readFileSync('./html/about.html');
    console.log('sending read file: about.html');
    this.res.end(data);
});

In the homepage there is a link/request for 'about', when a user clicks on that this router will send the about.html back to the client side. Our first part of this advanced httpserver application completed. Will come soon with the next part.
 
Recommended Reading

  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 08 March 2013 - 04:49 AM

You may consider creating an index page if the amount of tutorials keeps growing. 
(Like http://forum.codecal...index/?hl=index)


  • 1

#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 13 March 2013 - 09:02 AM

You may consider creating an index page if the amount of tutorials keeps growing. 
(Like http://forum.codecal...index/?hl=index)

Thanks for the advice -- hopefully I'll put one in place soon.


  • 0





Also tagged with one or more of these keywords: node, node.js, http server, flatiron