Expressjs:按下按钮时的运行方法

Expressjs: Run method when button gets pressed

如果我网站上的按钮被按下,我想 运行 我的节点服务器上的一个功能:

我还没有: Index.html(为简单起见,我没有添加整个网站)

<button id="tv">tv</button>

Client.js(客户端)

const tv = document.getElementById('tv');
volup.addEventListener('click', function(e) {
  fetch('/tv', {method: 'POST'})
});

Index.js(服务器端)(Client.js & Index.html 位于 "public" 文件夹

var express = require('express');
const app = express();
app.use(express.static('public'));
app.listen(80, function () {
   console.log('Webserver running!');
});
app.post('/tv', (req, res) => {
  console.log('it works');
})

我的解决方案是否有意义,或者是否有更好的解决方案。直到它还在工作,但在按下按钮几次后,服务器端的日志不再出现。

我很感激任何建议:)

您没有从 post 发送回复,因此您正在累积未完成的请求。

尝试让您的 post 处理程序发送响应,例如:

app.post('/tv', (req, res) => {
    console.log('it works');
    res.sendStatus(200);
});