Express.js App Delete Method



The app.delete() method routes all the HTTP DELETE requests to the specified path with the specified callback functions.

Syntax

app.delete(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 act like a middleware except that these callbacks can invoke next (route).

Example 1

Create a file "appDelete.js" and copy the following code snippet. After creating the file, use the command "node appDelete.js" to run this code.

// app.delete() 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; // Creating a DELETE request app.delete('/api', (req, res) => { console.log("DELETE Request Called for /api endpoint") res.send("DELETE Request Called") }) // App listening on the below port app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });

Hit the following Endpoint with a DELETE request

Output

C:\home
ode>> node appDelete.js Server listening on PORT 3000 DELETE Request Called for /api endpoint

Example 2

Let’s take a look at one more example.

// app.delete() 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; // Creating a DELETE request app.delete('/api', (req, res) => { console.log("Method called is -- ", req.method) res.end() }) // App listening on the below port app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });

Hit the following Endpoint with a DELETE request

Output

C:\home
ode>> node appDelete.js Server listening on PORT 3000 Method called is -- DELETE
Updated on: 2021-09-30T12:09:47+05:30

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements