HI WELCOME TO SIRIS

Express.js GET Request

Leave a Comment
GET and POST both are two common HTTP requests used for building REST API's. GET requests are used to send only limited amount of data because data is sent into header while POST requests are used to send large amount of data because data is sent in the body.
Express.js facilitates you to handle GET and POST requests using the instance of express.

Express.js GET Method Example 1

Fetch data in JSON format:
Get method facilitates you to send only limited amount of data because data is sent in the header. It is not secure because data is visible in URL bar.
Let's take an example to demonstrate GET method.
File: index.html
  1. <html>  
  2. <body>  
  3. <form action="http://127.0.0.1:8081/process_get" method="GET">  
  4. First Name: <input type="text" name="first_name">  <br>  
  5. Last Name: <input type="text" name="last_name">  
  6. <input type="submit" value="Submit">  
  7. </form>  
  8. </body>  
  9. </html>  
File: get_example1.js
  1. var express = require('express');  
  2. var app = express();  
  3. app.use(express.static('public'));  
  4.   
  5. app.get('/index.html', function (req, res) {  
  6.    res.sendFile( __dirname + "/" + "index.html" );  
  7. })  
  8. app.get('/process_get', function (req, res) {  
  9. response = {  
  10.        first_name:req.query.first_name,  
  11.        last_name:req.query.last_name  
  12.    };  
  13.    console.log(response);  
  14.    res.end(JSON.stringify(response));  
  15. })  
  16. var server = app.listen(8000, function () {  
  17.   
  18.   var host = server.address().address  
  19.   var port = server.address().port  
  20.   console.log("Example app listening at http://%s:%s", host, port)  
  21.   
  22. })  
Get Request 1
Open the page index.html and fill the entries:
Get Request 2
Now, you get the data in JSON format.
Get Request 3 Get Request 4

Express.js GET Method Example 2

Fetch data in paragraph format
File: index.html
  1. <html>  
  2. <body>  
  3. <form action="http://127.0.0.1:8000/get_example2" method="GET">  
  4. First Name: <input type="text" name="first_name"/>  <br/>  
  5. Last Name: <input type="text" name="last_name"/><br/>  
  6. <input type="submit" value="Submit"/>  
  7. </form>  
  8. </body>  
  9. </html>  
File: get_example2.js
  1. var express = require('express');  
  2. var app=express();  
  3. app.get('/get_example2', function (req, res) {  
  4. res.send('<p>Username: ' + req.query['first_name']+'</p><p>Lastname: '+req.query['last_name']+'</p>');  
  5. })  
  6. var server = app.listen(8000, function () {  
  7.   var host = server.address().address  
  8.   var port = server.address().port  
  9.   console.log("Example app listening at http://%s:%s", host, port)  
  10. })  
Get Request 5
Open the page index.html and fill the entries:
Get Request 6
Output:
Get Request 7

Express.js GET Method Example 3

File:index.html
  1. <!DOCTYPE html>  
  2. <html>  
  3. <body>  
  4. <form action="http://127.0.0.1:8000/get_example3">    
  5. <table>    
  6. <tr><td>Enter First Name:</td><td><input type="text" name="firstname"/><td></tr>    
  7. <tr><td>Enter Last Name:</td><td><input type="text" name="lastname"/><td></tr>    
  8. <tr><td>Enter Password:</td><td><input type="password" name="password"/></td></tr>    
  9. <tr><td>Sex:</td><td>  
  10. <input type="radio" name="sex" value="male"> Male  
  11. <input type="radio" name="sex" value="female">Female  
  12. </td></tr>    
  13. <tr><td>About You :</td><td>  
  14. <textarea rows="5" cols="40" name="aboutyou" placeholder="Write about yourself">  
  15. </textarea>  
  16. </td></tr>    
  17. <tr><td colspan="2"><input type="submit" value="register"/></td></tr>    
  18. </table>    
  19. </form>   
  20. </body>  
  21. </html>  
File: get_example3.js
  1. var express = require('express');  
  2. var app=express();  
  3.   
  4. app.get('/get_example3', function (req, res) {  
  5. res.send('<p>Firstname: ' + req.query['firstname']+'</p>  
  6. <p>Lastname: '+req.query['lastname']+'</p><p>Password: '+req.query['password']+'</p>  
  7. <p>AboutYou: '+req.query['aboutyou']+'</p>');  
  8. })  
  9.   
  10. var server = app.listen(8000, function () {  
  11.   var host = server.address().address  
  12.   var port = server.address().port  
  13.   console.log("Example app listening at http://%s:%s", host, port)  
  14. })  
Get Request 7 Get Request 8 Get Request 9

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.