Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Express.js GET


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. app.get(‘/index.html’, function (req, res) {
  5.    res.sendFile( __dirname + “/” + “index.html” );
  6. })
  7. app.get(‘/process_get’, function (req, res) {
  8. response = {
  9.        first_name:req.query.first_name,
  10.        last_name:req.query.last_name
  11.    };
  12.    console.log(response);
  13.    res.end(JSON.stringify(response));
  14. })
  15. var server = app.listen(8000, function () {
  16.   var host = server.address().address
  17.   var port = server.address().port
  18.   console.log(“Example app listening at http://%s:%s”, host, port)
  19. })

expressjsgetrequest1

Open the page index.html and fill the entries:

expressjsgetrequest2

Now, you get the data in JSON format.

expressjsgetrequest3

expressjsgetrequest4

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. })

Open the page index.html and fill the entries:

expressjsgetrequest6

Output:

expressjsgetrequest7

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. app.get(‘/get_example3’, function (req, res) {
  4. res.send(‘<p>Firstname: ‘ + req.query[‘firstname’]+'</p>
  5. <p>Lastname: ‘+req.query[‘lastname‘]+’</p><p>Password: ‘+req.query[‘password‘]+’</p>
  6. <p>AboutYou: ‘+req.query[‘aboutyou‘]+’</p>’);
  7. })
  8. var server = app.listen(8000, function () {
  9.   var host = server.address().address
  10.   var port = server.address().port
  11.   console.log(“Example app listening at http://%s:%s”, host, port)
  12. })

expressjsgetrequest8

expressjsgetrequest9

expressjsgetrequest10

Comments

comments