Most Popular Tutorials
Most Popular Tutorials :-

Simply Easy Learning at Your Fingertips. Click Tutorials Menu to view More Tutorial List





How to submit form in node.js?

when you are submit the form data in node.js then First install ExpressJs. here, i will explain very simple example of form submission with node.js express.

File Name : create project directory. eg. node_project

c:\node_project>

First create Html Form for submitting the data.

File Name : index.html

<!DOCTYPE html>
<html lang="en">
<head>
<title>Node JS Form Data Submission</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form action="/" method="POST">
<div class="form-group">
<label for="Name">Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter Name" name="name">
</div>

<div class="form-group">
<label for="Email1">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter Email">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</body>
</html>

create node.js page myapp.js

File Name : myapp.js

const express = require('express');
var bodyParser = require('body-parser')
const app = express();

var urlencodedParser = bodyParser.urlencoded({ extended: false })

app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});

app.post('/', urlencodedParser, (req, res) => {
console.log('Got body:', req.body);
res.sendStatus(200);
});
app.listen(3000);

Open Terminal or CMD

File Name :

c:\node_project> node myapp.js

Test on browser :-

File Name :

localhost:3000

Output :-

  • show the html form index.php
  • fill up the form and submit the form
  • See the result on console and browser.





  • Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here