提交表单时404 Not Found,但重新加载相同的URL时有效

时间:2014-07-08 16:14:20

标签: javascript node.js express

我使用ExpressJs制作了一个简单的登录系统,在提交表格时,用户将被引导至www.mywebsite.com/submit,并查看" Not Found 404"但是当我刷新同一页面时,我会看到相同的视图。

我不确定为什么会这样,但这里是代码

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 Not Found Error

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)

2 个答案:

答案 0 :(得分:0)

我猜表单是通过POST提交的,但您只为GET

定义了路由

答案 1 :(得分: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