tags:

views:

551

answers:

1

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

+4  A: 

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)

Maushu
Thanks Maushu. One correction, it is "response.close();" instead "response.end();"...
intellidiot
From 0.1.90 it's `response.end()`, IIRC
Kuroki Kaze