
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Express JS App Method in Method
app.METHOD() is used for mapping or routing an HTTP request where METHOD represents the HTTP method of the request such as GET, POST, PUT, etc., but in lowercase. Therefore, the methods are app.get(), app.post(), app.get(), and so on.
Syntax
app.METHOD(path, callback, [callback])
Parameters
path − This is the path for which the middleware function is invoked. A path can be a string, path pattern, a regular expression or an array of all these.
callback − These are the middleware functions or a series of middleware functions that acts like a middleware except that these callbacks can invoke next (route).
Example 1
Create a file "appMethod.js" and copy the following code snippet. After creating the file, use the command "node appMethod.js" to run this code.
// app.METHOD() Method Demo Example // Importing the express module const express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express var router = express.Router(); var PORT = 3000; // This api will handle the GET request app.get('/api', function(req, res){ res.send("GET method called"); }); // This api will handle the POST request app.post('/api', function(req, res){ res.send("POST method called"); }); // This api will handle the PUT request app.put('/api', function(req, res){ res.send("PUT method called"); }); app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Hit the following Endpoint to get the response
Output
C:\home
ode>> node appMethod.js Server listening on PORT 3000
On the browser, you will get to see the following output