【问题标题】:Cannot POST / error using express无法使用快递发布/出错
【发布时间】:2016-06-26 03:53:16
【问题描述】:

我正在尝试使用 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>

这是我的 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);

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

【问题讨论】:

  • 你的 / 是你的主页加载...例如,你尝试过在 /test 中发帖吗?

标签: node.js express


【解决方案1】:
You should call router instead of app
const router = express.Router();

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

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

【讨论】:

    【解决方案2】:

    就我而言,我没有注意我发帖的网址。

    我有页面登录 http://localhost:5000/login 正在发布,如果成功,则呈现页面帐户 http://localhost:5000/account

    但是,在这两个页面上玩了几次,链接仍然在http://localhost:5000/account,但显示了登录页面。因此,当我单击具有formmethod = "post" 的登录按钮时,我收到了错误Cannot POST /account

    所以修复是通过输入登录页面的 url http://localhost:5000/login 来正确呈现登录页面。

    【讨论】:

      【解决方案3】:

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

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

      例如:

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

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

      【讨论】:

      • 分号注释是不必要的,这是风格问题。
      【解决方案4】:

      这种方式你应该试试

      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);
      

      您应该记住的事项以备将来参考:

      1. 您将 url 编码扩展为 true
      2. 您的表单没有任何获取请求
      3. 您使用的是 HTML 命名变量,这是一种不好的做法

      感谢和欢呼

      【讨论】:

      • 谢谢!这确实有效。也非常感谢您的提示。
      • 谢谢,节省了我一个月的搜索时间
      【解决方案5】:

      似乎在这里工作正常。

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

      extended: true;

      您需要删除末尾的分号。

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

      【讨论】:

      • 当你指出这个问题时,你并没有给出一个写得很好的回复。我建议你更新你的帖子。
      • action="/" 并非没有后果。只能在提交页面也在服务器根目录下才能移除,这可能不是真的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 2019-08-19
      • 2021-07-31
      • 1970-01-01
      相关资源
      最近更新 更多