Easy Tutorial
❮ Nodejs Http Server Nodejs Global Object ❯

Node.js GET/POST Requests

In many scenarios, our server needs to interact with the user's browser, such as form submissions.

Form submissions to the server typically use GET/POST requests.

This section will introduce Node.js GET/POST requests.


Retrieving GET Request Content

Since GET requests are embedded directly in the path, the URL is the complete request path, including the part after the '?', so you can manually parse the content after the '?' as GET request parameters.

The parse function in Node.js's url module provides this functionality.

Example

var http = require('http');
var url = require('url');
var util = require('util');

http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
    res.end(util.inspect(url.parse(req.url, true)));
}).listen(3000);

Visit http://localhost:3000/user?name=tutorialpro.org&url=www.tutorialpro.org in the browser and view the returned result:

Retrieving URL Parameters

We can use the url.parse method to parse parameters in the URL, as shown below:

Example

var http = require('http');
var url = require('url');
var util = require('util');

http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type': 'text/plain'});

    // Parse url parameters
    var params = url.parse(req.url, true).query;
    res.write("Website Name: " + params.name);
    res.write("\n");
    res.write("Website URL: " + params.url);
    res.end();

}).listen(3000);

Visit http://localhost:3000/user?name=tutorialpro.org&url=www.tutorialpro.org in the browser and view the returned result:


Retrieving POST Request Content

The content of POST requests is entirely in the request body. The http.ServerRequest object does not have a property for the request body because waiting for the request body to be transmitted can be a time-consuming task.

For example, uploading files, and in many cases, we may not need to deal with the content of the request body. Malicious POST requests can greatly consume server resources, so Node.js does not parse the request body by default. You need to do it manually when necessary.

Basic Syntax Structure Explanation

var http = require('http');
var querystring = require('querystring');
var util = require('util');

http.createServer(function(req, res){
    // Define a post variable to temporarily store the request body information
    var post = '';     

    // Use the data event listener function of req to accumulate the request body data into the post variable
    req.on('data', function(chunk){    
        post += chunk;
    });

    // After the end event is triggered, parse post using querystring.parse to convert it into the true POST request format, then return it to the client.
    req.on('end', function(){    
        post = querystring.parse(post);
        res.end(util.inspect(post));
    });
}).listen(3000);

The following example submits a form via POST and outputs the data:

Example

var http = require('http');
var querystring = require('querystring');

var postHTML = 
  '<html><head><meta charset="utf-8"><title>tutorialpro.org Node.js Example</title></head>' +
<body>
  <form method="post">
    Website Name: <input name="name"><br>
    Website URL: <input name="url"><br>
    <input type="submit">
  </form>
</body></html>
http.createServer(function (req, res) {
  var body = "";
  req.on('data', function (chunk) {
    body += chunk;
  });
  req.on('end', function () {
    // Parse parameters
    body = querystring.parse(body);
    // Set response header and encoding
    res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'});

    if(body.name && body.url) { // Output submitted data
        res.write("Website Name: " + body.name);
        res.write("<br>");
        res.write("Website URL: " + body.url);
    } else {  // Output form
        res.write(postHTML);
    }
    res.end();
  });
}).listen(3000);

Execution Result Gif Demonstration:

❮ Nodejs Http Server Nodejs Global Object ❯