Node.JS Express Framework II

This is the second part of the article on node.js express framework. If you havent read the first part, I would advise you to check that article first and then come back and study this article. In previous article we saw how to send response back to an HTTP request. Apart from that you can also send static resources such as images, javascript files and stylesheets. Suppose you placed your images in a folder named “images” in the root directory. You can provide access to this directory via “expapp.use(express.static(‘images’))” command. Take a look at the following example.

var express = require('express');
var expapp = express();

expapp.use(express.static('images'));

expapp.get('/', function (req, res) {
   res.send('Welcome to knowledge hills');
})

var rserver = expapp.listen(9006, function () {
   var hostaddr = rserver.address().address
   var portaddr = rserver.address().port

   console.log("Server running at http://%s:%s", hostaddr, portaddr)

})

Download the code Try the code

In the above example we used express.static(“images”) middleware command and pass it the path that we want to be accessible by the client. Now anything inside the “images” folder is directly accessible via URL. Remember to test this example, create a folder named images inside the directory that contains your code file. The folder “images” and your code file should be at same level inside the directory. Now place any image named “pic1.jpg” inside the “images” folder. Type the following url in your browser “http://127.0.0.1:9006/pic1.jpg”. You shall see the image within your browser.

Handling Get Request From Forms

Till now, we have been hand coding the path in the browser to which server sends a response. However, we can also create form, fill some data in the form and then generate request via that form. The node.js server created via express framework shall also respond to that. Create a file named “main.htm” with following html script.

<html>
   <body>
      
      <form action = "http://127.0.0.1:9006/process_student" method = "GET">
         Student Name: <input type = "text" name = "student_name">  <br>
         Student Age: <input type = "text" name = "student_age">
         <input type = "submit" value = "Submit">
      </form>
      
   </body>
</html>

Download the code Try the code

Notice that in the above form the method used is GET and the URL in the action is 127.0.0.1:9006/process_student. Now you need to create node.js express app that can handle requests at this URL, and process them. The following code, simply display the name and age of the student passed via form, on the resultant web page. Take a look at the following code.

var express = require('express');
var expapp = express();

expapp.get('/process_student', function (req, res) {
   // Prepare output in JSON format
   response = {
      student_name:req.query.student_name,
      student_age:req.query.student_age
   };
   console.log(response);
   res.end(JSON.stringify(response));
})

var rserver = expapp.listen(9006, function () {
   var hostaddr = rserver.address().address
   var portaddr = rserver.address().port
   console.log("Server running at http://%s:%s", hostaddr, portaddr)

})

Download the code Try the code

Run the above code. And then fill the form that takes student name and age as input. Submit the form. A page shall open that will contain name and age entered by you in form. This shows that we have successfully created a node.js express framework app that listens to get request generated via form.

<<< Node.JS Express FrameworkNode.JS RESTful API >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .