【问题标题】:Cannot GET /url error in Node Js Post Handling在 Node Js Post 处理中无法获取 /url 错误
【发布时间】:2021-08-17 14:53:58
【问题描述】:

我写了这个名为Post_handling.ejs的ejs文件:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>POST-Handling Page</title>
  </head>
  <body>
    
<h1>Register here!!!</h1>

<form class="" method="POST" action="/Post_handling">
  <label for="">City:</label>
  <input type="text" name="city" value="">

  <label for="">State:</label>
  <input type="text" name="state" value="">

<input type="submit" name="submit" value="Submit">
</form>

  </body>
</html>

我已经编写了这个 node.js 代码来处理 POST 请求:

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var urlencodedParser = bodyParser.urlencoded({ extended: false });

app.set('view engine', 'ejs');



app.post('/Post_handling', urlencodedParser, function(request, response){
  response.render('Post_handling');
  console.log(request.body);
});

app.listen('3000');
console.log('Hello user! Now you are listening on port 3000 at address 127.0.0.1:3000\nPress ctrl+C to stop.');

当我运行 node.js 代码时,我得到这个错误:

无法 GET /Post_handling

【问题讨论】:

    标签: javascript html node.js express


    【解决方案1】:
    app.listen('3000');
    

    您是否访问了正确的端口?尝试指定完整的 url(带端口)

    【讨论】:

    • 是的,它是 3000 端口。
    • 我的意思是,尝试直接在
      中指定
    【解决方案2】:

    您在代码中使用 POST 方法,但尝试使用 GET 方法访问它?试试这个:

    app.get('/Post_handling', urlencodedParser, function(request, response){
      response.render('Post_handling');
      console.log(request.body);
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-15
    • 2012-05-24
    • 2020-12-11
    • 1970-01-01
    • 2018-06-22
    • 2020-03-26
    • 1970-01-01
    • 2016-04-07
    相关资源
    最近更新 更多