Node.js 在收到POST数据之前,如何防止Heroku上已部署的NodeJS web应用退出?

Node.js 在收到POST数据之前,如何防止Heroku上已部署的NodeJS web应用退出?,node.js,http,post,heroku,web-applications,Node.js,Http,Post,Heroku,Web Applications,我在基于服务器的应用程序方面非常缺乏经验(在客户端应用程序方面只稍微有点经验)。然而,基于大量示例(这里主要是关于堆栈溢出),我创建了一个基本的NodeJS web应用程序,在本地运行时可以正常工作(例如“heroku local web”)。但部署时,除非我插入延迟,否则应用程序不会收到发布的数据。我肯定我做错了什么,所以我希望有人能告诉我正确的处理方法。我已经将应用程序提炼为基本问题,仍然使用下面的代码显示 基本上,据我所知,这个简单的网页将JSON数据发布到我的web应用程序中。web应用

我在基于服务器的应用程序方面非常缺乏经验(在客户端应用程序方面只稍微有点经验)。然而,基于大量示例(这里主要是关于堆栈溢出),我创建了一个基本的NodeJS web应用程序,在本地运行时可以正常工作(例如“heroku local web”)。但部署时,除非我插入延迟,否则应用程序不会收到发布的数据。我肯定我做错了什么,所以我希望有人能告诉我正确的处理方法。我已经将应用程序提炼为基本问题,仍然使用下面的代码显示

基本上,据我所知,这个简单的网页将JSON数据发布到我的web应用程序中。web应用程序确实收到了POST消息。但在web应用程序可以收集正在传输的数据之前,web应用程序似乎已退出。当应用程序在本地运行时,数据不会丢失(当我在谷歌云上部署早期版本时,数据也不会丢失)。但在Heroku(自由层)上部署时,确实会发生丢失

由于应用程序中没有延迟退出的代码(例如,消息泵或等待退出信号),因此使用其他几种语言的经验让我感到困惑,为什么NodeJS应用程序在收到任何消息之前并不总是退出。当它起作用时,我只是耸耸肩,并决定在游戏中有一些魔术,我将在以后学习。但实际上,我看到的失败对我来说更有意义

在任何情况下,正是这种怀疑导致我添加了延迟(添加了以下带有//***注释的行)。从这一点,我可以确定数据已经到达,但没有被收集,没有延迟

下面是主要的代码文件。下面是一些日志,显示添加延迟前后的本地和远程行为

谢谢你的帮助

index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang='en'>
<meta http-equiv='Content-Language' content='en'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>

<script>

var scriptlocation = location.href.replace(document.location.pathname, '');
var request = new XMLHttpRequest();

function OnReadyStateChange() {
  if (request.readyState == 4 && request.status == 200) {
    console.log("Data upload complete.");
  }
}

function SendDataToServer(data) {
  var url = scriptlocation + "/server.js";
  request.open('POST', url, true);
  request.setRequestHeader('Content-type', 'application/json');
  request.onreadystatechange = OnReadyStateChange;
  request.send(data);
}

function SendEntryDataToServer() {
  var entryData = {
    fruit: "apple",
    size: "medium",
    color: "red"
  };
  console.log("*** SENDING DATA TO SERVER ***");
  var postData = JSON.stringify(entryData);
  SendDataToServer(postData);
  return false;
}

</script>
</head>
<body>
  <form>
    <input type="button" onclick="SendEntryDataToServer()" value="Post Data"/>
  </form>
</body>
增加延迟前的本地:

10:24:00 PM web.1 |  FILE end
10:24:00 PM web.1 |  Server is running (port: 5000)...
10:24:14 PM web.1 |  req.url: /index.html
10:24:14 PM web.1 |  GET
10:24:18 PM web.1 |  req.url: /server.js
10:24:18 PM web.1 |  POST
10:24:18 PM web.1 |  POST end
10:24:18 PM web.1 |  { fruit: 'apple', size: 'medium', color: 'red' }
2019-12-18T04:31:52.835329+00:00 app[web.1]: FILE end
2019-12-18T04:31:52.837439+00:00 app[web.1]: Server is running (port: 17378)...
2019-12-18T04:32:14.929250+00:00 heroku[router]: at=info method=GET path="/index.html" host=****.herokuapp.com request_id=**** fwd="*.*.*.*" dyno=web.1 connect=0ms service=9ms status=200 bytes=1233 protocol=https
2019-12-18T04:32:14.925381+00:00 app[web.1]: req.url: /index.html
2019-12-18T04:32:14.925407+00:00 app[web.1]:
2019-12-18T04:32:14.925509+00:00 app[web.1]: GET
2019-12-18T04:32:25.004774+00:00 app[web.1]: req.url: /server.js
2019-12-18T04:32:25.004809+00:00 app[web.1]:
2019-12-18T04:32:25.004964+00:00 app[web.1]: POST
2019-12-18T04:32:25.006023+00:00 app[web.1]: POST end
10:48:04 PM web.1 |  FILE end
10:48:04 PM web.1 |  Server is running (port: 5000)...
10:48:08 PM web.1 |  req.url: /index.html
10:48:08 PM web.1 |  GET
10:48:12 PM web.1 |  req.url: /server.js
10:48:12 PM web.1 |  POST
10:48:12 PM web.1 |  Sleeping 5 seconds
10:48:12 PM web.1 |  { fruit: 'apple', size: 'medium', color: 'red' }
10:48:17 PM web.1 |  POST end
2019-12-18T04:51:50.925802+00:00 app[web.1]: req.url: /index.html
2019-12-18T04:51:50.925831+00:00 app[web.1]:
2019-12-18T04:51:50.925944+00:00 app[web.1]: GET
2019-12-18T04:51:56.071684+00:00 heroku[router]: at=info method=POST path="/server.js" host=****.herokuapp.com request_id=**** fwd="*.*.*.*" dyno=web.1 connect=0ms service=6ms status=200 bytes=151 protocol=https
2019-12-18T04:51:56.064644+00:00 app[web.1]: req.url: /server.js
2019-12-18T04:51:56.064659+00:00 app[web.1]:
2019-12-18T04:51:56.068033+00:00 app[web.1]: POST
2019-12-18T04:51:56.069013+00:00 app[web.1]: Sleeping 5 seconds
2019-12-18T04:51:56.075197+00:00 app[web.1]: { fruit: 'apple', size: 'medium', color: 'red' }
2019-12-18T04:52:01.073243+00:00 app[web.1]: POST end
在添加延迟之前部署:

10:24:00 PM web.1 |  FILE end
10:24:00 PM web.1 |  Server is running (port: 5000)...
10:24:14 PM web.1 |  req.url: /index.html
10:24:14 PM web.1 |  GET
10:24:18 PM web.1 |  req.url: /server.js
10:24:18 PM web.1 |  POST
10:24:18 PM web.1 |  POST end
10:24:18 PM web.1 |  { fruit: 'apple', size: 'medium', color: 'red' }
2019-12-18T04:31:52.835329+00:00 app[web.1]: FILE end
2019-12-18T04:31:52.837439+00:00 app[web.1]: Server is running (port: 17378)...
2019-12-18T04:32:14.929250+00:00 heroku[router]: at=info method=GET path="/index.html" host=****.herokuapp.com request_id=**** fwd="*.*.*.*" dyno=web.1 connect=0ms service=9ms status=200 bytes=1233 protocol=https
2019-12-18T04:32:14.925381+00:00 app[web.1]: req.url: /index.html
2019-12-18T04:32:14.925407+00:00 app[web.1]:
2019-12-18T04:32:14.925509+00:00 app[web.1]: GET
2019-12-18T04:32:25.004774+00:00 app[web.1]: req.url: /server.js
2019-12-18T04:32:25.004809+00:00 app[web.1]:
2019-12-18T04:32:25.004964+00:00 app[web.1]: POST
2019-12-18T04:32:25.006023+00:00 app[web.1]: POST end
10:48:04 PM web.1 |  FILE end
10:48:04 PM web.1 |  Server is running (port: 5000)...
10:48:08 PM web.1 |  req.url: /index.html
10:48:08 PM web.1 |  GET
10:48:12 PM web.1 |  req.url: /server.js
10:48:12 PM web.1 |  POST
10:48:12 PM web.1 |  Sleeping 5 seconds
10:48:12 PM web.1 |  { fruit: 'apple', size: 'medium', color: 'red' }
10:48:17 PM web.1 |  POST end
2019-12-18T04:51:50.925802+00:00 app[web.1]: req.url: /index.html
2019-12-18T04:51:50.925831+00:00 app[web.1]:
2019-12-18T04:51:50.925944+00:00 app[web.1]: GET
2019-12-18T04:51:56.071684+00:00 heroku[router]: at=info method=POST path="/server.js" host=****.herokuapp.com request_id=**** fwd="*.*.*.*" dyno=web.1 connect=0ms service=6ms status=200 bytes=151 protocol=https
2019-12-18T04:51:56.064644+00:00 app[web.1]: req.url: /server.js
2019-12-18T04:51:56.064659+00:00 app[web.1]:
2019-12-18T04:51:56.068033+00:00 app[web.1]: POST
2019-12-18T04:51:56.069013+00:00 app[web.1]: Sleeping 5 seconds
2019-12-18T04:51:56.075197+00:00 app[web.1]: { fruit: 'apple', size: 'medium', color: 'red' }
2019-12-18T04:52:01.073243+00:00 app[web.1]: POST end
本地延迟后添加:

10:24:00 PM web.1 |  FILE end
10:24:00 PM web.1 |  Server is running (port: 5000)...
10:24:14 PM web.1 |  req.url: /index.html
10:24:14 PM web.1 |  GET
10:24:18 PM web.1 |  req.url: /server.js
10:24:18 PM web.1 |  POST
10:24:18 PM web.1 |  POST end
10:24:18 PM web.1 |  { fruit: 'apple', size: 'medium', color: 'red' }
2019-12-18T04:31:52.835329+00:00 app[web.1]: FILE end
2019-12-18T04:31:52.837439+00:00 app[web.1]: Server is running (port: 17378)...
2019-12-18T04:32:14.929250+00:00 heroku[router]: at=info method=GET path="/index.html" host=****.herokuapp.com request_id=**** fwd="*.*.*.*" dyno=web.1 connect=0ms service=9ms status=200 bytes=1233 protocol=https
2019-12-18T04:32:14.925381+00:00 app[web.1]: req.url: /index.html
2019-12-18T04:32:14.925407+00:00 app[web.1]:
2019-12-18T04:32:14.925509+00:00 app[web.1]: GET
2019-12-18T04:32:25.004774+00:00 app[web.1]: req.url: /server.js
2019-12-18T04:32:25.004809+00:00 app[web.1]:
2019-12-18T04:32:25.004964+00:00 app[web.1]: POST
2019-12-18T04:32:25.006023+00:00 app[web.1]: POST end
10:48:04 PM web.1 |  FILE end
10:48:04 PM web.1 |  Server is running (port: 5000)...
10:48:08 PM web.1 |  req.url: /index.html
10:48:08 PM web.1 |  GET
10:48:12 PM web.1 |  req.url: /server.js
10:48:12 PM web.1 |  POST
10:48:12 PM web.1 |  Sleeping 5 seconds
10:48:12 PM web.1 |  { fruit: 'apple', size: 'medium', color: 'red' }
10:48:17 PM web.1 |  POST end
2019-12-18T04:51:50.925802+00:00 app[web.1]: req.url: /index.html
2019-12-18T04:51:50.925831+00:00 app[web.1]:
2019-12-18T04:51:50.925944+00:00 app[web.1]: GET
2019-12-18T04:51:56.071684+00:00 heroku[router]: at=info method=POST path="/server.js" host=****.herokuapp.com request_id=**** fwd="*.*.*.*" dyno=web.1 connect=0ms service=6ms status=200 bytes=151 protocol=https
2019-12-18T04:51:56.064644+00:00 app[web.1]: req.url: /server.js
2019-12-18T04:51:56.064659+00:00 app[web.1]:
2019-12-18T04:51:56.068033+00:00 app[web.1]: POST
2019-12-18T04:51:56.069013+00:00 app[web.1]: Sleeping 5 seconds
2019-12-18T04:51:56.075197+00:00 app[web.1]: { fruit: 'apple', size: 'medium', color: 'red' }
2019-12-18T04:52:01.073243+00:00 app[web.1]: POST end
添加延迟后部署:

10:24:00 PM web.1 |  FILE end
10:24:00 PM web.1 |  Server is running (port: 5000)...
10:24:14 PM web.1 |  req.url: /index.html
10:24:14 PM web.1 |  GET
10:24:18 PM web.1 |  req.url: /server.js
10:24:18 PM web.1 |  POST
10:24:18 PM web.1 |  POST end
10:24:18 PM web.1 |  { fruit: 'apple', size: 'medium', color: 'red' }
2019-12-18T04:31:52.835329+00:00 app[web.1]: FILE end
2019-12-18T04:31:52.837439+00:00 app[web.1]: Server is running (port: 17378)...
2019-12-18T04:32:14.929250+00:00 heroku[router]: at=info method=GET path="/index.html" host=****.herokuapp.com request_id=**** fwd="*.*.*.*" dyno=web.1 connect=0ms service=9ms status=200 bytes=1233 protocol=https
2019-12-18T04:32:14.925381+00:00 app[web.1]: req.url: /index.html
2019-12-18T04:32:14.925407+00:00 app[web.1]:
2019-12-18T04:32:14.925509+00:00 app[web.1]: GET
2019-12-18T04:32:25.004774+00:00 app[web.1]: req.url: /server.js
2019-12-18T04:32:25.004809+00:00 app[web.1]:
2019-12-18T04:32:25.004964+00:00 app[web.1]: POST
2019-12-18T04:32:25.006023+00:00 app[web.1]: POST end
10:48:04 PM web.1 |  FILE end
10:48:04 PM web.1 |  Server is running (port: 5000)...
10:48:08 PM web.1 |  req.url: /index.html
10:48:08 PM web.1 |  GET
10:48:12 PM web.1 |  req.url: /server.js
10:48:12 PM web.1 |  POST
10:48:12 PM web.1 |  Sleeping 5 seconds
10:48:12 PM web.1 |  { fruit: 'apple', size: 'medium', color: 'red' }
10:48:17 PM web.1 |  POST end
2019-12-18T04:51:50.925802+00:00 app[web.1]: req.url: /index.html
2019-12-18T04:51:50.925831+00:00 app[web.1]:
2019-12-18T04:51:50.925944+00:00 app[web.1]: GET
2019-12-18T04:51:56.071684+00:00 heroku[router]: at=info method=POST path="/server.js" host=****.herokuapp.com request_id=**** fwd="*.*.*.*" dyno=web.1 connect=0ms service=6ms status=200 bytes=151 protocol=https
2019-12-18T04:51:56.064644+00:00 app[web.1]: req.url: /server.js
2019-12-18T04:51:56.064659+00:00 app[web.1]:
2019-12-18T04:51:56.068033+00:00 app[web.1]: POST
2019-12-18T04:51:56.069013+00:00 app[web.1]: Sleeping 5 seconds
2019-12-18T04:51:56.075197+00:00 app[web.1]: { fruit: 'apple', size: 'medium', color: 'red' }
2019-12-18T04:52:01.073243+00:00 app[web.1]: POST end

万一后来有人遇到这个问题,我没有得到任何反馈,也找不到合理的解决办法。(我尝试了Promissions和其他一些特别的信令。)因此,我避开了这一点,使用Express作为我的服务器,因为我知道这已经成功地在全球部署。考虑到极其简单的数据传输,切换非常简单,而且工作正常。(我本打算深入研究Express源代码,看看他们是如何处理的,但似乎在我的NodeJS应用程序中包含它会导入200多个其他包,因此这让我很沮丧。)