How To Accept The Request With Json Data In Node.js?
Node.js is an open-source and cross-platform runtime environment built on Chrome'due south V8 JavaScript engine for executing JavaScript lawmaking outside of a browser. You need to recollect that NodeJS isn't a framework, and it's not a programming language. In this article, we will talk over how to make post request using node.js
POST is a asking method supported past HTTP that sends information to the server. In express, we can use the app.post() method to accept a Mail service request. The basic syntax to use the app.post() method is mentioned below.
The post data is provided to us on the req object inside the callback function of the app.post() method. Nosotros tin can access the data sent equally the trunk using the syntax mentioned below.
const bodyContent = req.body;
Similarly, if nosotros want to access the header content then we tin do so using the syntax mentioned beneath.
const headerContent = req.headers;
Project Setup:
Step ane: Install Node.js if Node.js is not installed in your car.
Step 2: Create a folder for your projection and created two files named app.js and alphabetize.html inside of it.
Step 3: Now, initialize a new Node.js projection with default configurations using the post-obit control on the control line.
npm init -y
Pace 4: Now install express inside your project using the following control on the command line.
npm install express
Project Structure: After post-obit the steps your project structure volition look like.
Filename: app.js
Javascript
const express = require(
'express'
);
const app = express();
app.use(limited.json());
app.get(
'/'
, (req, res) => {
res.sendFile(__dirname +
'/index.html'
);
});
app.post(
'/'
, (req, res) => {
const { username, password } = req.body;
const { authorisation } = req.headers;
res.ship({
username,
countersign,
authority,
});
});
app.listen(3000, () => {
panel.log(
'Our express server is up on port 3000'
);
});
Filename: index.html
HTML
<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-viii"
/>
<
title
>POST DEMO</
championship
>
</
head
>
<
trunk
>
<
form
>
<
div
>
<
characterization
>Username</
characterization
>
<
input
type
=
"text"
id
=
"user"
/>
</
div
>
<
div
>
<
label
>Password</
label
>
<
input
type
=
"countersign"
id
=
"pass"
/>
</
div
>
<
push
type
=
"submit"
>Submit</
button
>
</
form
>
<
script
>
document.querySelector('button')
.addEventListener('click', (e) => {
e.preventDefault();
const username = document
.querySelector('#user').value;
const password = document
.querySelector('#pass').value;
fetch('/', {
method: 'POST',
headers: {
Say-so: 'Bearer abcdxyz',
'Content-Type': 'awarding/json',
},
torso: JSON.stringify({
username,
password,
}),
})
.then((res) => {
return res.json();
})
.then((data) => console.log(data));
});
</
script
>
</
body
>
</
html
>
In the to a higher place example, we take created an express server that renders the index.html file. This alphabetize.html contains a form that has two inputs as username and countersign. When we press the submit push it sends a Post request to the home road with the body containing the username and countersign and the header containing the dominance token. We handle this mail request inside our app.post() method and send these details i.e., the username, password, and authority token as a response. Nosotros later print these details to the console.
Run app.js file using below command:
node app.js
Output: Open up the browser and become to http://localhost:3000 and you will see the following output.
How To Accept The Request With Json Data In Node.js?,
Source: https://www.geeksforgeeks.org/how-to-get-post-data-in-node-js/
Posted by: stinsonhavelf.blogspot.com
0 Response to "How To Accept The Request With Json Data In Node.js?"
Post a Comment