javascript - 节点服务器自行重启
问题描述
所以我有这个文件“client.html”,它将一个字符串发送到节点服务器
客户端.html
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="http://127.0.0.1:3000/search" method='get'>
<input type="text" name="name">
<input type="submit" value="search">
</form>
</body>
</html>
服务器获取输入字符串并在控制台上打印出来。一段时间后,它应该打印“其他东西”。
index.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var util = require('util');
var port = process.env.PORT || 3000;
app.all('/', function(req, res) {
res.sendFile(__dirname+'/client.html');
});
app.get('/search', function(req, res) {
var name = req.query.name;
console.log(name);
setTimeout(function(){
console.log("Something else");
}, 240000);
}) ;
//listen in a specific port
app.listen(port);
//check status
console.log('Server running at http://localhost:' + port);
相反,“name”变量被打印出来,然后过了一段时间,在打印出“其他东西”之前,“name”变量再次被打印出来,就好像有人点击了表单中的发送按钮一样客户。需要一些帮助来解决这个问题
解决方案
我想您应该在连接超时并且浏览器尝试重新连接之前回复客户端:
app.get('/search', function(req, res) {
var name = req.query.name;
console.log(name);
setTimeout(function(){
console.log("Something else");
}, 240000);
res.end("Searching ...");
}) ;
推荐阅读
- r - 有没有办法在一行中创建一个具有多个因子级别的新 df 特征?
- python - 如果日期时间对象的格式不正确,Python3 如何引发 ValueError?
- algorithm - 我有关于脚本 C 的家庭作业并创建 IF Else 语句
- python - 如何在python中的颜色之间平滑过渡?
- scala - Akka 流 actor-conflation-ratelimit-actor 丢弃前几条消息(有时)
- java - 在邮递员中发送有效负载时,有没有办法从另一个表的主键中插入外键值的值?
- android - 为什么 Job 服务的 onStartJob 方法是每秒调用一次?
- python - 如果数据框中的大多数列相等,则 Pandas 设置值
- javascript - 有没有办法从另一个孩子调用子组件的函数?
- maven - Maven不更新依赖