POST request and Node.js without a nerve

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

+4
source share
1 answer

By default, the http.Server Node.js class 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 port 8000, which will be the echo method used in the request.

If you want to receive POST, you should simply check request.method for the string "POST".

<h / "> Update regarding response.end :

Starting with version 0.1.90, the function of closing the response is response.end instead of response.close . In addition to changing the name, end can also send data and close the response after sending this data, as opposed to closing. ( api example )

+7
source

Source: https://habr.com/ru/post/1306783/


All Articles