Friday 19 October 2018

node.js: http: Handle post requests

In this post, I am going to explain how to submit a post request and read the post body in node.js application.

Below steps explain the application in brief.
a.   User access the registration form by hitting the below url.

b.   Once user submits the form, the data received by node.js application and send the successful message.

registration.html
<html>

<head>
    <title>User Registration Form</title>
</head>

<body>
    <form action="registerMe" method = "post">
        <h1>User Registration Form</h1>
        <p>Please fill in this form to create an account.</p>
        <hr>

        <label for="email"><b>Email</b></label>
        <input type="text" placeholder="Enter Email" name="email" required>

        <label for="password"><b>Password</b></label>
        <input type="password" placeholder="Enter Password" name="password" required>

        <label for="repeatPassword"><b>Repeat Password</b></label>
        <input type="password" placeholder="Repeat Password" name="repeatPassword" required>
        <hr>
        <button type="submit" class="registerbtn">Register</button>
    </form>
</body>

</html>

HelloWorld.js
var http = require('http');
var fs = require('fs');
var path = require('path');

var server = http.createServer((request, response) => {
    console.log(`Requested Resource: ${request.url}`);
    if (request.url === '/') {
        response.writeHead(200, {
            'Content-Type': 'text/html'
        });

        response.end('<a href="/registration">Register Me</a>');
    } else if (request.url === '/registration') {
        var htmlFile = path.join(__dirname, 'registration.html');

        response.writeHead(200, {
            'Content-Type': 'text/html'
        });

        var readableStream = fs.createReadStream(htmlFile, {
            encoding: 'utf-8'
        });

        readableStream.pipe(response);
    } else if (request.url === '/registerMe') {
        var formData = "";

        request.on('data', (content) => {
            console.log(content.toString());
            formData += content.toString();
        });

        response.writeHead(200, {
            'Content-Type': 'text/plain'
        });

        /* Once all the request data is received, send the response to client */
        request.on('end', function() {
            console.log('formData : ' + formData);
            response.end('formData' + formData);
        });

    } else {
        console.log(`${request.url} Resource not available`);
        response.writeHead(404, {
            'Content-Type': 'text/plain'
        });
        response.end('Resource not available');
    }

});

server.listen(9000);

console.log('Server started and listening on port 9000');


Run HelloWorld.js nd hit the url ‘http://localhost:9000/’, you can see below screen.


Click on the hyper link ‘Register Me’, you will be navigated to registration form.

Fill the details and click on Register button.


Previous                                                 Next                                                 Home

No comments:

Post a Comment