Express.js – app.delete() 方法
app.delete() 方法会将所有 HTTP DELETE 请求路由到指定路径,并带有指定回调函数。
语法
app.delete(path, callback, [callback])
参数
- path − 这是触发中间件函数的路径。路径可以是字符串、路径模式、正则表达式或这些项的数组。
- callback − 这是中间件函数或一系列中间件函数,它们的作用类似于中间件,只不过这些回调函数可以调用 next(route)。
示例 1
创建一个文件“appDelete.js”,并复制以下代码片段。创建文件后,使用命令“node appDelete.js”运行此代码。
// 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);
});访问以下端点,发送 DELETE 请求
输出
C:\home
ode>> node appDelete.js Server listening on PORT 3000 DELETE Request Called for /api endpoint
示例 2
让我们看另一个示例。
// 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);
});访问以下端点,发送DELETE请求
输出
C:\home
ode>> node appDelete.js Server listening on PORT 3000 Method called is -- DELETE
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP