Example method GET in express

1. create file get.html:

 

<html>

   <body>

     

      <form action = "http://127.0.0.1:8081/process_get" method = "GET">

         First Name: <input type = "text" name = "first_name">  <br>

         Last Name: <input type = "text" name = "last_name">

         <input type = "submit" value = "Submit">

      </form>

     

   </body>

</html>

 

2. file express_method_get.js

 

var express = require('express');

var app = express();

 

app.use(express.static('public'));

app.get('/index.html', function (req, res) {

    res.sendFile(__dirname + "/" + "get.html");

});

 

app.get('/process_get', function (req, res) {

    // Prepare output in JSON format

    response = {

        first_name: req.query.first_name,

        last_name: req.query.last_name

    };

    console.log(response);

    res.end(JSON.stringify(response));

});

 

var server = app.listen(8080, function () {

    var host = server.address().address;

    var port = server.address().port;

    console.log("Server listening at http://%s:%s", host, port);

 

});

 

Run server and go browser http://localhost:8080/index.html

Input first name, last name then submit: