如何使用Node.js中的Express处理发布请求?我写了以下不起作用的代码



无法使用node.js中的express处理发布请求数据。我想从test.js文件中从index.html文件中处理表单数据。获取错误:发布/测试"错误(404(:"找不到"

以下是我编写的代码:

index.html

<html>
    <body>
            <form action="/test" method="POST">
                Name: <input type="text" name="name"/>
                <input type="submit" value="Submit"/>
                </form>
    </body>
</html>

test.js

var express = require("express");
var bodyParser = require('body-parser');
var app = express();
var urlEncodedParser = app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: false
}));
app.post('/test',urlEncodedParser, function(req,res){
console.log("Kushagra "+req.body);
});
app.listen(8080,function(){
  console.log("Started in port 8080");
});

为了从index.html文件接收表单数据到test.js文件。

// TODO: Place this code before route & put index.html to public directory

const path = require("path");

app.use(express.static(path.join(__dirname, "public")));

相关内容

最新更新