How to Post HTML Form Data in NodeJS or ExpressJS Server with Architecture and Source Code

preview_player
Показать описание
How to Post HTML Form Data in NodeJS or ExpressJS Server

Once you start the project on your localhost:3000, following happens on the backend –

1. Browser makes GET request to our backend NodeJS Server for route ‘/’.
2. Once our server receives this request, it starts looking for suitable controller for this route.
5. Once a user submits a form, a POST request (using form’s attribute called ‘method’) is sent to the same route with all form data.
7. That’s all.

Рекомендации по теме
Комментарии
Автор

can we get data from the form with using only node js without express

najihashim
Автор

I am trying to access form data using GET method. Can anyone tell me what is the error in the below code:

import express from "express";

const app=express()
const port = process.env.PORT || '4000'

app.use(express.urlencoded({
extended: false
}));
app.use(express.json());

app.get('/', (req, res) => {
res.send('Home Page');
})

app.get('/form', (req, res) => {
// res.setHeader('content-type',
res.write('<h1>Student Registration Form</h1>')
res.write('<h1>Student Registration Form</h1>')
res.write('<form method="GET" action="/handleForm" >')
res.write('<input type="text" value="name" placeholder="Name" /> <br/><br/>')
res.write('<input type="number" value="age" placeholder="Age" /> <br/><br/>')
res.write('<input type="submit" value="submit"/>')
res.write('<form/>')
res.send();
})

app.get('/handleForm', (req, res) => {
console.log(req.query)
res.send('Form Submitted')
})

app.listen(port, () => {
})

amartyajaiswal