2014-07-08 35 views
0

我使用ExpressJs創建了一個簡單的登錄系統,並在提交表單時將用戶定向到www.mywebsite.com/submit並查看「Not Found 404」但是當我刷新同一頁時,我看到了該視圖。404未找到表單提交但在重新加載相同URL時工作

我不知道原因是什麼,但這裏是代碼

Index.js(路線)

var express = require('express'); 
var router = express.Router(); 

router.get('/', function(req, res) { 
res.render('index', { title: 'Express' }); 
}); 

router.get('/submit', function(req, res){ 
res.render('submit'); 
}); 

module.exports = router; 

Submit.jade

extends layout 

block content 
h1 Successfully Logged In 

404未找到錯誤

Error: Not Found at Layer.app.use.res.render.message [as handle (/Applications/MAMP/htdocs/nodejs/app.js:29:15) 
    at trim_prefix (/Applications/MAMP/htdocs/nodejs/node_modules/express/lib/router/index.js:240:15) 
    at /Applications/MAMP/htdocs/nodejs/node_modules/express/lib/router/index.js:208:9 
    at Function.proto.process_params (/Applications/MAMP/htdocs/nodejs/node_modules/express/lib/router/index.js:269:12) 
    at next (/Applications/MAMP/htdocs/nodejs/node_modules/express/lib/router/index.js:199:19) 
    at next (/Applications/MAMP/htdocs/nodejs/node_modules/express/lib/router/index.js:176:38) 
    at /Applications/MAMP/htdocs/nodejs/node_modules/express/lib/router/index.js:137:5 
    at /Applications/MAMP/htdocs/nodejs/node_modules/express/lib/router/index.js:250:10 
    at next (/Applications/MAMP/htdocs/nodejs/node_modules/express/lib/router/index.js:160:14) 
    at next_layer (/Applications/MAMP/htdocs/nodejs/node_modules/express/lib/router/route.js:77:14) 
+0

您需要通過調用'router.post()定義POST方法的路徑' – nullability

回答

0

我猜的形式被通過POST提交,但已定義的路由只對GET

+0

是的,它是通過POST提交,但如何具體定義POST的路線? – user3651267

+0

@ user3651267檢查api [app.VERB](http://expressjs.com/api.html#app.VERB) –

+0

router.post不工作​​,但當我試圖改變form method =「get」並嘗試與路由器.get,它工作。 我想方法發佈。 – user3651267

0
router.get('/submit', function(req, res){ 
    res.render('submit'); 
}); 

應該

router.post('/submit', function(req, res){ 
    res.render('submit'); 
}); 

根據文檔:

app.VERB()方法在Express中提供路由功能, 其中VERB是HTTP動詞之一,例如app.post()。

來源:http://expressjs.com/api.html#app.VERB

相關問題