Node.js http.IncomingMessage.method Method
The http.IncomingMessage.method is an inbuilt application programming interface of class Incoming Message within the inbuilt http module which is used to get the type of request method as a string.
Syntax:
request.method
Parameters: This method does not accept any argument as a parameter.
Return Value: This method returns the request type name as a string.
Example 1: Filename: index.js
Javascript
// Node.js program to demonstrate the // request.method Method // Importing http module var http = require('http'); // Setting up PORT const PORT = process.env.PORT || 3000; // Creating http Server var httpServer = http.createServer( function(request, response){ // Getting request method // by using request.method method const value = request.method; // Display result on the console response.end( "method : " + value, 'utf8', () => { console.log("displaying the result..."); // Closing the server httpServer.close(()=>{ console.log("server is closed") }) }); }); // Listening to http Server httpServer.listen(PORT, () => { console.log("Server is running at port 3000..."); }); |
Execution command:
node index.js
Console Output:
Server is running at port 3000... displaying the result... displaying the result... server is closed server is closed
Browser Output: Paste the localhost address http://localhost:3000/ in the search bar of the browser.
Example 2: Filename: index.js
Javascript
// Node.js program to demonstrate the // request.method Method // Importing http module var http = require('http'); // Request and response handler functionconst http2Handlers = (request, response) => { // Getting request method // by using request.method Api const value = request.method; // Display result response.end( "method : " + value , 'utf8', () => { console.log("displaying the result..."); // Closing the server httpServer.close(()=>{ console.log("server is closed") }) });}; // Creating http Server and listening // on the given port numbervar httpServer = http.createServer( http2Handlers).listen(3000, () => { console.log("Server is running at port 3000..."); }); |
Execution command:
node index.js
Console Output:
Server is running at port 3000... displaying the result... displaying the result... server is closed server is closed
Browser Output: Now run http://localhost:3000/ in the browser.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_message_method


