简体   繁体   English

NodeJS 和 MongoDB“无法 POST /”

[英]NodeJS and MongoDB "Cannot POST /"

Been using mongoDB with the mongoose ODM and I can't seem to figure it out.一直将 mongoDB 与 mongoose ODM 一起使用,但我似乎无法弄清楚。 I've been following a tutorial online, but when I test out my code, it gets redirected to an empty page that says "Cannot POST /"我一直在在线学习教程,但是当我测试我的代码时,它被重定向到一个空白页面,上面写着“无法 POST /”

Here is my code:这是我的代码:

server.js服务器.js

var mongoose = require("mongoose");
var bodyParser = require("body-parser");
var express = require("express");
var app = express();


var PORT = 3332;

app.use("/", express.static(__dirname));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));


mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/gamedata", {
    useNewUrlParser: true
});


var gameSchema = new mongoose.Schema({
    nickname: String
});

var User = mongoose.model("User", gameSchema);


app.post("/addname", (req, res) =>{

    var playerNickname = new User(req.body);
    playerNickname.save()
    .then(item => {

        console.log("nickname created")
        res.send("item saved to database");


    })
    .catch(err => {

        res.status(400).send("unable to save to database");
        console.log("error baby!");

    });
});

app.listen(PORT, function () {
    console.log("server is up and running using port " + PORT)
});

index.html索引.html

<html>
<link href="https://fonts.googleapis.com/css?family=Press+Start+2P&display=swap" rel="stylesheet">

<body>
<h1 class="center-align">Create Nickname</h1>
    <form method="post" name="/addname">
        <input id="pickName" type='text' name='pickName' placeholder='Nickname' required>
        <input id='ready' value=" Ready >" type='submit'>
    </form>

</body>
<script>
</script>

</html>

What seems to fix it is when i change当我改变时,似乎可以解决它

app.use("/", express.static(__dirname));

to the following code:到以下代码:

app.use("/", (req, res) => {
    res.sendFile(__dirname + "/index.html");
});

But in my specific case i cannot do this.但在我的具体情况下,我不能这样做。

Does anyone know of a work around?有谁知道解决方法?

You should change your index.html你应该改变你的index.html

Insteady of use:代替使用:

 <form method="post" name="/addname">

you should use:你应该使用:

<form method="post" action="/addname">

This should solve your problem.这应该可以解决您的问题。


The Action Attribute动作属性

The action attribute defines the action to be performed when the form is submitted. action 属性定义提交表单时要执行的操作。

Usually, the form data is sent to a page on the server when the user clicks on the submit button.通常,当用户单击提交按钮时,表单数据会发送到服务器上的页面。

In the example above, the form data is sent to a page on the server called "/addname".在上面的示例中,表单数据被发送到名为“/addname”的服务器上的页面。 This page contains a script that handles the form data:此页面包含处理表单数据的脚本:

If the action attribute is omitted, the action is set to the current page.如果省略action属性,则将操作设置为当前页面。

The attribute "name" is used in input fields, not in the tag.属性“名称”用于输入字段,而不是标签。 I just found some information here.我刚刚在这里找到了一些信息。 https://www.w3schools.com/html/html_forms.asp https://www.w3schools.com/html/html_forms.asp

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

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