Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6325763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:57:24+00:00 2026-05-24T16:57:24+00:00

In an ExpressJS set up on top of NodeJS, I have a bit of

  • 0

In an ExpressJS set up on top of NodeJS, I have a bit of code like this:

app.get('/keys/readresults/:keyname', basic_auth, function(req,res){
    res.writeHead(200, {'content-type': 'text/json'});
    setInterval(
        go_to_database_and_get('stuff', function (database_replies) {
        res.write(database_replies)
        })
    ,5000);
});

The code is wrote like that for simplicity (if anyone wants the real code I’m happy to post it in some pastebin).

What happens when I curl -u username:password http://localhost:4000/keys/readresults/key is exactly what I wanted to happen: I get a 200 status header, it checks the database for any results I want and writes the response back to me, then waits 5 seconds and searches for new results in the database and writes the response again, and so on every interval of 5 seconds.

The problem is that when I exit curl, the loop keeps on going forever. How do I tell express or nodejs that it’s supposed to end that loop (with clearInterval I guess) as soon as the request has ended ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:57:24+00:00Added an answer on May 24, 2026 at 4:57 pm

    req.on("close")

    So simply

    app.get('/keys/readresults/:keyname', basic_auth, function(req,res){
        res.writeHead(200, {'content-type': 'text/json'});
        var no = setInterval(
            go_to_database_and_get('stuff', function (database_replies) {
            res.write(database_replies)
        });
        ,5000);
        req.on("close", function() {
            clearInterval(no);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Expressjs w/ node.js and I have a route like this: users.init(app.db, function()
Is there a way to restrict allowed uri characters in Expressjs. like set a
I have an expressjs app with everyauth setup. Everyauth is working fine. Now, I
How can i bind a expressjs server to a specific IP Something like app.listen(8888,
I'm starting to experiment with nodejs/expressjs/coffeescript and the jade view engine. I have the
In my Node/Express.js project I can set the views folder globally like so: app.configure(function()
If I run my expressjs app like so: coffee server.coffee and navigate to localhost:8080
Based on the response to this question: How do I configure nodejs/expressjs to serve
I have set up the full-text search functionality in SQL Server 2008 express. This
On POSTing data to my expressjs app, this is what I am getting: node(58287,0x7fff771ad960)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.