简体   繁体   English

不能GET / POST? 快递错误?

[英]Cannot GET /POST? error in express?

I have a very simple express code 我有一个非常简单的快递代码

var express = require("express");
var bodyParser = require("body-parser");
var http = require("http");
var path = require("path");

var app = express();
app.use(bodyParser.urlencoded({ extended: false }));

app.set("views",path.resolve(__dirname,"views"));
app.set("view engine","ejs");

app.get("/",function(req,res){
    res.render("index");
});

app.post('/', function(req, res) {
    console.log("posted!");
    res.render("final");
});



http.createServer(app).listen(3000);

Now when I try visiting localhost:3000 everything loads fine, in index.ejs I have a simple form with a input having a name mname, whan i hit a name in the input box and press enter I get This error 现在,当我尝试访问localhost:3000时,一切都正常加载,在index.ejs中我有一个简单的表单,输入名称为mname,我在输入框中输入一个名字然后按回车我得到这个错误

Cannot GET /POST?mname=a

I defined a app.post in the code, and asked it to render final.ejs. 我在代码中定义了一个app.post,并要求它呈现final.ejs。 So where is the code going wrong? 那么代码出错了什么地方?

tried other questions saying that express 4 users "router" for routing, tried that but also failed. 尝试了其他问题,说快递4用户“路由器”进行路由,试过但也失败了。

You did not include the ejs file with the form you want to submit, but from the error you get it seems you are not doing a POST request, but instead a GET request to path /POST . 您没有将ejs文件包含在您要提交的表单中,但是从错误中您可以看到您没有执行POST请求,而是对路径/POST发出GET请求。 This is a completely different thing. 这是完全不同的事情。

I guess in the form you have something like: 我想在形式上你有类似的东西:

 <form action='POST'>

but instead you need something like: 但你需要这样的东西:

<form action='/' method='POST'>

我刚遇到了类似的问题,我解决了它,因为我忘了将app.get包含在我/ post链接中。

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

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