How can i put a loop in this server

前端 未结 3 1200
臣服心动
臣服心动 2021-01-25 01:15

For example, something similar i want to do:

var http = require(\'http\');

const PORT=8080;
var myvar = \"Test: \";

function handleRequest(request, response){
         


        
相关标签:
3条回答
  • 2021-01-25 01:35

    Javascript in node.js is single threaded and event driven. Incoming events can only get processed when the current piece of Javascript executing is done and has returned back to the system.

    As such, you can't just loop on something forever with a while loop because then no other Javascript in your server can run. No events can be processed which is death for a server that is supposed to process incoming requests in a timely manner.

    Node.js is an event-driven system. Activities must be scheduled via events (timers, networking callbacks, other async callbacks, etc...). Any long running loop blocks the entire server from doing anything else.

    If you want to regular check the state of some pin and your underlying hardware driver does not provide an event-driven interface, then you will probably want to use setInterval() or setTimeout() to "poll" the state of the hardware. Just make sure you don't make the interval between polling so short that you don't leave much time for your server to do other things.

    // poll the pin state
    setInterval(function() {
        let pinState = getPin();
        if (pinState === someValue) {
            // do something
        }
    }, 100);
    

    As an example of this, I have a Raspberry Pi running node.js that serves as an attic fan controller. It has two temperature probes hooked into it (one for outside air temp and one for attic air temp). It uses a setInterval() every 10 seconds to check those two temperature values and make a decision whether the attic fans should be turned on or off.

    Ideally you would avoid polling on a timer in node.js because it's inefficient as heck. You'd rather have an underlying driver that lets you register a callback for some significant change in the pin state or hardware device. But, sometimes your drivers don't do this so you have to restort to old-fashioned "polling".

    0 讨论(0)
  • 2021-01-25 01:37

    If you just want to continuously process data, you can use the setInterval function, which will continuously call a function with a small interval between. This will give the rest of your code time to run.

    // synchronous code, with a while loop
    while(true) {
      // do something
    }
    // NOTE: anything after this never runs
    
    // asynchronous code, with setInterval
    setInterval(function() { 
      // do something
    }, 10);
    

    10 here is the number of milliseconds between each time the code is run. For most purposes (such as continuously reading for a pin), a small number (such as 10 milliseconds, or 100 times per second) should work fine.

    0 讨论(0)
  • 2021-01-25 01:47

    I'm not sure but you can try to send request to your own node server via http or use restler node module.

    function recursive() {
        http.request(url, function(err, res) {
            recursive();
        })
    }
    

    https://nodejs.org/api/http.html#http_http_request_options_callback

    And call this method when you want to start your recursion.

    0 讨论(0)
提交回复
热议问题