我正在嘗試生成一個動態 URL,每次呼叫端點時都會更改所以我在 Node.js 中有這個簡單的函式來創建一個動態 url:
var path = crypto.createHash('md5').update(`${Date.now()}`).digest("hex");
function createNewPath(){
path = crypto.createHash('md5').update(`${Date.now()}`).digest("hex");
}
這個簡單的代碼來接收請求:
app.use('/' path, function(req,res){
createNewPath();
res.send("<h1>Welcome!<h1>");
});
問題是 app.use 不會重新加載“路徑”中的值,因此分配的初始 url 保持不變,有什么想法嗎?
一直在打破我的頭,一無所獲
uj5u.com熱心網友回復:
不要動態創建路由。提前設定。檢查其中的哈希值。
創建一個將哈希作為引數的路由。
如果不匹配則回傳錯誤。
app.get("/:hash", (req, res) => {
if (req.params.hash === path) {
return res.send("<h1>Welcome!<h1>");
}
return res.sendStatus(404);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/449377.html
標籤:javascript 节点.js 表示 节点模块
