Node.js Express-将POST请求的响应从客户端发送回客户端

时间:2020-08-06 08:22:46

标签: javascript html node.js express

我正在处理HTML表单,将数据处理到服务器(使用express和nodejs)并返回到客户端。以下是我的程序。

//My folder structure
-index.js
-public
 -index.html
 -timer.html
-...
  1. HTML表单(index.html)将POST请求发送到服务器
<form method="POST" action="/timer">
 <input type="text" name="name">
 <button type="submit">Submit</button>
</form>
  1. 服务器可以接收POST请求,我可以console.log(req.body)。 (工作)
  2. 然后,我想通过发送html文件(公用文件夹下的timer.html)和数据将数据传递回客户端。 (无效)
app.post('/timer', urlencodedParser, (req,res) => {
  const data = req.body;
  res.sendFile('public/timer.html' , { root : __dirname, data: req.body});
})
  1. 如果可行,如何在timer.html(文件发送回客户端)中显示变量(数据)?

非常感谢!

0 个答案:

没有答案