How to process POST data in Node.js?

Internet of Things IoT Frameworks 3 years ago

3.53K 1 0 0 0

_x000D_ _x000D_ How do you extract form data (form[method="post"]) and file uploads sent from the HTTP POST method in Node.js? I've read the documentation, googled and found nothing. function (request, response) { //request.post???? } Is there a library or a hack?

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago
_x000D_ If you use Express (high-performance, high-class web development for Node.js), you can do this: HTML:
API client: fetch('/', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ user: { name: "John", email: "john@example.com" } }) }); Node.js: (since Express v4.16.0) // Parse URL-encoded bodies (as sent by HTML forms) app.use(express.urlencoded()); // Parse JSON bodies (as sent by API clients) app.use(express.json()); // Access the parse results as request.body app.post('/', function(request, response){ console.log(request.body.user.name); console.log(request.body.user.email); }); Node.js: (for Express <4.16.0) const bodyParser = require("body-parser"); /** bodyParser.urlencoded(options) * Parses the text as URL encoded data (which is how browsers tend to send form data from regular forms set to POST) * and exposes the resulting object (containing the keys and values) on req.body */ app.use(bodyParser.urlencoded({ extended: true })); /**bodyParser.json(options) * Parses the text as JSON and exposes the resulting object on req.body. */ app.use(bodyParser.json()); app.post("/", function (req, res) { console.log(req.body.user.name) });
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum


Q

Azure IoT hub and sending messages with mosquitto_pub

_x000D_ _x000D_ I'm trying to send some simple message with mosquitto_pub to Azure IoT HUB but faced...
Q

How to generate AWS bootstrap certificates with java SDK?

_x000D_ _x000D_ According to this docs I need to generate so-called bootstrap certificates for my Io...
Q

is it possible to do 3way handshake only one time with mqtt communication?

_x000D_ _x000D_ I am using mosquitto_pub to publish the data with TLS using a topic. I am using mosq...

Important Internet of Things Links