开发者

POST request and Node.js without Nerve

开发者 https://www.devze.com 2022-12-26 10:11 出处:网络
开发者_如何学JAVAIs there any way to accept POST type requests without using Nerve lib in Node.js? By default the http.Server class of Node.js accepts any http method.

开发者_如何学JAVAIs there any way to accept POST type requests without using Nerve lib in Node.js?


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消