使用HTTP请求连续运行node.js文件 [英] Run a node.js file continuously using an HTTP request

查看:101
本文介绍了使用HTTP请求连续运行node.js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js并表示要创建一个应用程序,我有一个文件TCPServer.js,可用于不断轮询TCP/IP服务器.我希望能够将单个HTTP请求发送到读取IP地址和端口号的数据库,然后使用该数据调用我的TCPServer.js文件,该文件依次轮询TCP服务器.

I am using node.js and express to create an application, I have a file TCPServer.js that i use to continually poll a TCP/IP server. I want to be able to send a single HTTP request to a database that reads in the IP Address and port number, then uses that data to call my TCPServer.js file which in turn polls the TCP server.

读取数据库有效,HTTP请求仅适用于对TCPServer的一次调用,但是每当我尝试不断轮询TCP服务器时,我都会从服务器收到1轮询响应,然后抛出500错误.

Reading the database works, the HTTP request works for a single call to the TCPServer, but whenever I try and continually poll the TCP server i get a 1 poll response from the server then a 500 error thrown.

因此,如果在TCPServer.js中仅使用getInputData(ipAddress, connPort),则HTTP请求将没有问题,并从我的TCP Server返回一次响应,并返回200响应.使用setInterval(getInputData(ipAddress, connPort), 2000),我将获得一次数据和500错误响应.我可以每2秒轮询一次吗?

So if with just getInputData(ipAddress, connPort) in TCPServer.js then the HTTP request works no problem and returns the response from my TCP Server once and a 200 response. With setInterval(getInputData(ipAddress, connPort), 2000) I get the data once and a 500 error response. Can I get this to poll every 2 seconds?

TCPServer.js

TCPServer.js


function getInputData(ipAddress, port) {
          "Function for polling TCP Server runs in here"
}

const iModStart = function startInputModule(ipAddress, connPort) {
  setInterval(getInputData(ipAddress, connPort), 2000)

}

module.exports = iModStart

用于运行http请求的路由处理程序

route handler for running the http request


const iModuleConnect = require('../utils/TCPServer')

//run the polling to input module

router.get('/connections_connect/:id', async (req, res) => {
    const _id = req.params.id

    try {
        const connection = await Connection.findById(_id)
        if (!connection) {
            return res.status(404).send()
        } 
        if(connection) {
            console.log(connection.ipAddress)
            console.log(connection.port)
            iModuleConnect(connection.ipAddress, connection.port)

        }
        res.send(connection)       
    } catch(e) {
        res.status(500).send()
    }
})
module.exports = router

推荐答案

问题是setInterval要求将回调函数作为其第一个参数,但是您将已经执行的getInputData函数的结果传递给它.

The problem is that setInterval requires a callback function as its first argument, but you pass it the result of your already executed getInputData function.

很明显,这在函数在第一个间隔之前执行一次后起作用,但是一旦在2000 ms之后达到间隔,就不会调用任何回调,并且节点将抛出TypeError [ERR_INVALID_CALLBACK]: Callback must be a function错误.

Obviously this works once as the function is exectued before the first interval, but as soon as the interval is reached after 2000 ms, there is no callback to call, and node will throw an TypeError [ERR_INVALID_CALLBACK]: Callback must be a function error.

要解决此问题,您需要将函数参数绑定到getInputData并将其作为回调传递给setInterval:

To fix this you need to bind the function arguments to getInputData and pass this as a callback to setInterval:

const iModStart = function startInputModule(ipAddress, connPort) {
      setInterval(getInputData.bind(getInputData, ipAddress, connPort), 2000);
}

这篇关于使用HTTP请求连续运行node.js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆