node.js 无法使用 express 发布/错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35931135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:02:53  来源:igfitidea点击:

Cannot POST / error using express

node.jsexpress

提问by dtem052996

I am trying to create a simple form handler using express. I tried the code below for my form:

我正在尝试使用 express 创建一个简单的表单处理程序。我为我的表单尝试了以下代码:

<form class="form"  action="/" method="post" name="regForm">              
    <div class="form-group">
        <input type="text" name="username" class="form-control" id="username" placeholder="Username">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

And here is my app.js code:

这是我的 app.js 代码:

const port = 3000;

var express = require('express'),
    app = express(),
    server = require('http').createServer(app);
var bodyParser = require('body-parser');

app.use(express.static(__dirname + '/public'));

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

app.use(bodyParser.json());

app.post('/',function(req,res){
   var username = req.body.username;
   var html = 'Hello:' + username;
   res.send(html);
   console.log(html);
});

server.listen(port);

I keep getting the error "CANNOT POST /" after submitting the form. Am I missing something like a module?

提交表单后,我不断收到错误“无法发布 /”。我错过了像模块这样的东西吗?

回答by Amulya Kashyap

This way you should try

这种方式你应该试试

const port = 3000;

var express = require('express'),
    app = express();
var bodyParser = require('body-parser');

app.use(express.static(__dirname + '/public'));

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

app.use(bodyParser.json());

app.get('/', function(req, res){
  res.render('form');// if jade
  // You should use one of line depending on type of frontend you are with
  res.sendFile(__dirname + '/form.html'); //if html file is root directory
 res.sendFile("index.html"); //if html file is within public directory
});

app.post('/',function(req,res){
   var username = req.body.username;
   var htmlData = 'Hello:' + username;
   res.send(htmlData);
   console.log(htmlData);
});

app.listen(port);

Things you should keep in mind for future Ref :

你应该记住的事情,以备将来参考:

  1. You were extending url encode to true
  2. You were not having any get request for your form
  3. You were using HTML named variable which is one of bad practices
  1. 您正在将 url 编码扩展为 true
  2. 您的表单没有任何 get 请求
  3. 您正在使用 HTML 命名变量,这是一种不良做法

Thanks & Cheers

感谢和干杯

回答by chmod

Seems to work fine here.

似乎在这里工作正常。

The only bug I found is here:

我发现的唯一错误在这里:

extended: true;

extended: true;

You need to remove the semicolon at the end.

您需要删除末尾的分号。

Also, you don't need action="/" in your form tag, just FYI.

此外,您的表单标签中不需要 action="/",仅供参考。

回答by Samarth

Another Way is that we can use .route method provided by express

另一种方式是我们可以使用express提供的.route方法

https://expressjs.com/en/guide/routing.html

https://expressjs.com/en/guide/routing.html

Eg:

例如:

app.route("url")
   .get(function())

   .post(function());

remember to close the .route function using semi colon ;

记得使用分号关闭 .route 函数;