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