Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST request and Node.js without Nerve

Tags:

post

node.js

Is there any way to accept POST type requests without using Nerve lib in Node.js?

like image 882
intellidiot Avatar asked Apr 13 '10 11:04

intellidiot


People also ask

How do you use bodyParser without express?

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.

How do you handle a POST request in node JS?

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.

How do you access POST data in node JS?

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.


1 Answers

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".


Update regarding 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)

like image 85
Diogo Gomes Avatar answered Sep 22 '22 23:09

Diogo Gomes