nodejs-tw / ama

Ask me anything!
MIT License
31 stars 1 forks source link

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

Open e314520513 opened 8 years ago

e314520513 commented 8 years ago

目的

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 commented 8 years ago

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

dca commented 8 years ago

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

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

e314520513 commented 8 years ago

謝謝,最後放棄put跟 delete了