Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
e314520513 opened this issue Jun 21, 2016 · 3 comments
Open

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

e314520513 opened this issue Jun 21, 2016 · 3 comments

Comments

@e314520513
Copy link

目的

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
Copy link
Author

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

@dca
Copy link
Contributor

dca commented Jun 21, 2016

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

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

@e314520513
Copy link
Author

謝謝,最後放棄put跟 delete了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants