ama icon indicating copy to clipboard operation
ama copied to clipboard

node 在做route的時候遇到一個問題

Open e314520513 opened this issue 9 years ago • 3 comments

目的

node 在做route的時候遇到一個問題

使用的工具

node.js express4

遇到的問題

不知道res.redirect()要怎麼寫才能重導到router.get('/todo'

程式碼

router.get('/todo', function(req, res, next){

    res.send(todoList);
    res.end();
});

//create a todo list
router.post('/todo', function(req, res, next){

    var id = counter += 1;//todo list id
    todoList[id] = req.params();
    res.send(counter.toString());
    res.end();
});

router.delete('/todo/:id', function(req, res, next){

    var id = req.params('id');//todo list id
    todoList[id] = undefined;
    res.redirect('/todo');//想要重導回router.get('/todo,function(){}')
});

e314520513 avatar Jun 21 '16 08:06 e314520513

我發現 res.redirect();預設是get方法,不知道可不可以改變他的請求方法

e314520513 avatar Jun 21 '16 08:06 e314520513

res.redirect() 只是幫你在 Header 寫 Location ,基本上是瀏覽器幫你做 302 重新導向,所以只有 GET

ref. 原始碼(實際上是在前面的 res.location 設定了 Header) https://github.com/expressjs/express/blob/9375a9a/lib/response.js#L857

dca avatar Jun 21 '16 08:06 dca

謝謝,最後放棄put跟 delete了

e314520513 avatar Jun 25 '16 11:06 e314520513