Is there any way to accept POST type requests without using Nerve lib in Node.js?
To parse the POST request body in Node JS without using Express JS body-parser, We have to listen events, emitted by the request, ie. 'data' event and 'end' event. Above we are using Buffer. concat(), it is a method available in Node JS Buffer class, it join all the received chunks and returns a new Buffer.
Note: If you are going to make GET, POST request frequently in NodeJS, then use Postman , Simplify each step of building an API. In this syntax, the route is where you have to post your data that is fetched from the HTML. For fetching data you can use bodyparser package. Web Server: Create app.
The post data is provided to us on the req object inside the callback function of the app. post() method. We can access the data sent as the body using the syntax mentioned below. const bodyContent = req.
By default the http.Server class of Node.js accepts any http method.
You can get the method using request.method
(api link).
Example:
var sys = require('sys'),
http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write(request.method);
response.end();
}).listen(8000);
sys.puts('Server running at http://127.0.0.1:8000/');
This will create a simple http server on the port 8000 that will echo the method used in the request.
If you want to get a POST you should just check the request.method
for the string "POST".
response.end
:
Since version 0.1.90, the function to close the response is response.end
instead of response.close
. Besides the name change, end
can also send data and close the response after this data is sent unlike close. (api example)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With