简体   繁体   English

Express Server似乎给出200然后在发布请求时给出404

[英]Express Server seems to give 200 then gives 404 on post request

I am teaching myself NodeJS to create an application, and I'm running into a problem with Express where the server seems to return a 200 code on a post request, but then immediately becomes a 404 for some reason. 我正在教自己NodeJS创建一个应用程序,我遇到了Express的问题,服务器似乎在发布请求上返回200代码,但由于某种原因立即成为404。

The request was properly handled before I added a jquery method in between the client and server to do some front-end verification first. 在我在客户端和服务器之间添加jquery方法之前,请求已正确处理,以便先进行一些前端验证。 Before, I simply just 以前,我只是
included "action='/login'" as an attribute for the form tag and it worked. 包含“action ='/ login'”作为表单标记的属性并且它起作用。

I am using the express generator 我正在使用快速发电机

Here's how the form info looks like (in Jade): 这是表单信息的样子(在Jade中):

form(name="logins" id="loginfo" role="form" method="post")

It is handled by this function in welcome-screen.js, located in a static directory which I've specified in app.js 它由welcome-screen.js中的这个函数处理,位于我在app.js中指定的静态目录中

$(document).ready(function() {
    $("#loginfo").submit(function(event, done) {
        checkEmail();
    });
});

function checkEmail() {
    var email = document.forms["logins"]["usrname"].value;
    console.log(email);
    if (email.length != 0) {
        getLogin(email);
    }
}

function getLogin(email) {
    $.post("/login", {"usrname" : email});
}

Here's the server code which handles the post request on "/login". 这是在“/ login”上处理发布请求的服务器代码。 The console shows the print statements, so I know that the request is making it to the server and that "/login" can be found. 控制台显示打印语句,因此我知道请求正在向服务器发出,并且可以找到“/ login”。

var express = require('express');
var router = express.Router();
....
router.post("/login", function(req, res) {
    console.log(req.body["usrname"]);
    var user = req.body["usrname"];
    res.render("login", {title : "Hello!",
                    username : user});
    console.log("Passed rendering");
});

However, I am redirected to the default error page giving me a 404 error. 但是,我被重定向到默认错误页面,给出了404错误。

Here is the stack trace I got, if it helps anyone. 这是我得到的堆栈跟踪,如果它可以帮助任何人。

Error: Not Found
    at D:\projects\neg5\app.js:30:13
    at Layer.handle [as handle_request]      (D:\projects\neg5\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\projects\neg5\node_modules\express\lib\router\index.js:312:13)
at D:\projects\neg5\node_modules\express\lib\router\index.js:280:7
at Function.process_params (D:\projects\neg5\node_modules\express\lib\router\index.js:330:12)
at next (D:\projects\neg5\node_modules\express\lib\router\index.js:271:10)
at D:\projects\neg5\node_modules\express\lib\router\index.js:618:15
at next (D:\projects\neg5\node_modules\express\lib\router\index.js:256:14)
at Function.handle (D:\projects\neg5\node_modules\express\lib\router\index.js:176:3)
at router (D:\projects\neg5\node_modules\express\lib\router\index.js:46:12)

After I send the post request, the console prints these two lines included in the express generator, so I know that "/login" is being found. 发送邮件请求后,控制台打印快速生成器中包含的这两行,所以我知道正在找到“/ login”。

POST /login 200 34.463 ms - 598
POST / 404 25.479 ms - 1398

I've been pulling out my hair trying to figure out why this isn't working. 我一直在拔我的头发试图弄清楚为什么这不起作用。 Any help would be greatly appreciated! 任何帮助将不胜感激!

You need to add action="/login" to your login form (in views/index.jade ), like so: 您需要在登录表单中添加action="/login" (在views/index.jade ),如下所示:

form(name="logins" id="loginfo" action="/login" role="form" method="post")
    // rest of view omitted 
POST /login 200 34.463 ms - 598
POST / 404 25.479 ms - 1398

this tells two requests had been sent, one is /login, another is /. 这告诉两个请求已经发送,一个是/ login,另一个是/。

   POST /login 200 is trigger by code $.post()
   POST/ 404  is triggered by code $.submit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM