The example code below demonstrate creating a simple submit application for handling HTTP post request and get data. In order to do this first create an HTML file with the code as shown below.
form.html
[c]
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>SPLessons Registration</title>
</head>
<body>
<form action="/submit-student-data" method="post">
First Name: <input name="firstName" type="text" /> <br />
Last Name: <input name="lastName" type="text" /> <br />
Email Address: <input name="Email Address" type="text" /> <br />
Telephone Number: <input name="Telephone Number" type="text" /> <br />
<input type="submit" />
</form>
</body>
</html>
[/c]
Now, get the post request data by importing body-parser using the following code.
valid.js
[c]
var express = require('express');
var app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/form.html');
});
app.post('/submit-student-data', function (req, res) {
var name = req.body.firstName + ' ' + req.body.lastName;
res.send(name + ' Submission Completed!');
});
var server = app.listen(8080, function () {
console.log('Node server is running..');
});
[/c]
Compile the code using the command
[c]$ node valid.js [/c]
By using the command one can get the following output as shown in the image below.
Now, run on browser using the port number localhost:8080 by doing this one can get the output as shown in the image below.
Type first name like
SPLessons and last name like
Tutorials and then click the
Create Profile button, now the browser responds and display the message as shown in the image below.