Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
nodej尝试评估HTML表单_Html_Node.js_Express - Fatal编程技术网

nodej尝试评估HTML表单

nodej尝试评估HTML表单,html,node.js,express,Html,Node.js,Express,我在使用NodeJs和express评估HTML表单时遇到问题 这是我的Java脚本代码 我的目标是使用express在nodeJs中处理HTML表单 const express = require('express'); const http = require('http'); const fs = require('fs'); const app = express(); var warehouses = []; app.use(express.urlencoded({extended

我在使用NodeJs和express评估HTML表单时遇到问题

这是我的Java脚本代码

我的目标是使用express在nodeJs中处理HTML表单

const express = require('express');
const http = require('http');
const fs = require('fs');
const app = express();

var warehouses = [];

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

  app.use("/warehouse", (req, res, next) => {
    fs.readFile("./addWarehouse.html", function(err, data) {

    res.write(data);
    next();

    });

  });



  app.post("/warehouse/add", (req, res) => {


    console.log("ADDED");

    // warehouses.push(req.body.nWarehouse);
    console.log('Request Type:', req.method);
    res.end;
  });

app.listen(8080);



这是我的HTML表单

<!DOCTYPE html>

<!-- <head>
    <meta charset="utf-8" />
    <title>Coole Seite</title>

</head> -->
<body>
    <h1>Warehouses</h1>

    <form method='POST' action="/warehouse/add">
        <input type="text" name="nWarehouse"  id="nWarehouse"/>
        <input typse="submit" value="bitte sende" />
     </form>

</body>
</html>


仓库
我试着用控制台输出调试它,我发现它永远不会访问应用程序


我很乐意得到一些建议。

如果目的是评估
addWarehouse.html
中的表单,当您转到
/warehouse
时,该表单应提交给
/warehouse/add

这里根本不需要通过
app.use(…)
使用中间件概念

快速代码:

const express = require('express');
const http = require('http');
const fs = require('fs');
const app = express();

var warehouses = [];

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

//show addWareHouse.html for /warehouse
/* serving the HTML via fs */
app.get("/warehouse", (req, res, next) => {
  fs.readFile("./addWarehouse.html", function(err, data) {
    res.writeHead(200, { "Content-Type": "text/html" });
    res.write(data);
    res.end();
  });

//add warehouse form submit for /warehouse/add
app.post("/warehouse/add", (req, res) => {
  console.log("ADDED");
  console.log("REQUEST PARAM::", req.body);
  //do you adding of ware-house stuff here
  console.log("Request Type:", req.method);
  return res.end();
});

app.listen(8080, () => console.log(`app listening on port 8080!`));
注意:
在express中,还有其他方便的方式来提供视图(HTML),比如模板引擎,
res.sendFile()
等等。

您使用app.use(…)而不是app.post(…)?您可能想更改它,然后再试一次。我尝试过,但app.post()也失败了。您需要添加
app.listen(端口)
现在您的express server没有监听任何端口,之后您应该将路由更正为
app.post
,除非您希望此功能充当中间件。首先感谢Tom。我编辑了我的代码,但它仍然不起作用@tom@tomslabbaert
http.createServer(app).listen(8080);
这很好,因为尽管handy只是一个包装器。@Alex请求永远不会到达
/submit/add
,因为您在中间件
应用程序上结束请求
res.end()
。使用(“/submit”,…)