简体   繁体   English

如何修复 404 错误 Node JS Express POST 请求

[英]How to fix 404 error Node JS Express POST Request

I am trying to submit the following html form to a js file:我正在尝试将以下 html 表单提交到 js 文件:

                        <form action="/submit-form" method="POST">


                             <input type="text" name="username">

                             <button> Click Here Now </button>


                        </form>

Here´s the file I want to submit it to.这是我要提交的文件。 I've tried moving folders around, changing ports, and nothing seems to work.我试过移动文件夹,更改端口,但似乎没有任何效果。

          var express = require('express');

          var app = express();
          
          app.listen(80, function(){ 

           console.log("Hello whatsup");

         })
         app.post('/submit-form', function(req,response){

              console.log("All great");
    
          })

Both files are on my Xampp htdocs folder, specifically located inside a folder that contains made for this particular project.这两个文件都在我的 Xampp htdocs 文件夹中,特别位于包含为这个特定项目制作的文件夹中。 It contains the package.json files, the node_modules folder, the js file and the html form.它包含 package.json 文件、node_modules 文件夹、js 文件和 html 表单。

If you're just running form locally with no kind of hosting/rendering then you need to specify the url for it to know how to hit your local server.如果您只是在本地运行表单而不进行任何托管/渲染,那么您需要指定 url 以了解如何访问您的本地服务器。

Example 1:示例 1:

Using your code example but modified slightly to use non-relative path使用您的代码示例,但稍作修改以使用非相对路径

Let's say you create a file index.html in your machine and open it through browser with the following contents per your example above假设你在你的机器上创建了一个文件index.html并通过浏览器打开它,上面的例子中包含以下内容

<form action="http://localhost:8080/submit-form" method="POST">
  <input type="text" name="username">
  <button> Click Here Now </button>
</form>

The above assumes your server is setup as follows with port 8080. All I did was modify the port in your code以上假设您的服务器设置如下,端口为 8080。我所做的只是修改代码中的端口

Let's say you have a server.js file that you start as node server.js .假设您有一个以node server.js开头的 server.js 文件。 NOTE: This assumes that you already have express in your node_modules注意:这假设您的 node_modules 中已经有 express

      var express = require('express');

      var app = express();
      
      app.listen(8080, function(){ 

       console.log("Hello whatsup");

      })
      app.post('/submit-form', function(req,response){
          console.log("All great");

      })

Example 2:示例 2:

Using your code example to explain relative path使用您的代码示例来解释相对路径

If you are hosting both the server and client in the same server for which the url used for your index.html is the same as the url used for your server.如果您将服务器和客户端托管在同一台服务器中,并且用于您的 index.html 的 url 与用于您的服务器的 url 相同。 Then you can use the relative path as you had it然后你可以使用相对路径

<form action="/submit-form" method="POST">
  <input type="text" name="username">
  <button> Click Here Now </button>
</form>

Since this will look for the domain that you are rendering from.因为这将查找您正在渲染的域。 Let's say you are rendering and end up with a domain http://awesomeurl.com , then when your form hits /submit-form it will then call http://awesomeurl.com/submit-form with the POST request.假设您正在渲染并最终获得一个域http://awesomeurl.com ,然后当您的表单点击 /submit-form 时,它将使用 POST 请求调用http://awesomeurl.com/submit-form If your server is also hosted and exposed with same url, then it will all work.如果您的服务器也使用相同的 url 托管和公开,那么一切都会正常工作。

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

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