javascriptnode.jsmongodbexpressserver-application

Node.js app that runs without a user and stores data in a database


I'm trying to build a backend infrastructure that hits specific API endpoints repetitively on a given time duration, once every second for example. The system would then post (or whatever the appropriate equivalent to a post request would be) the response to my database so that I can work with the data at a later time.

I was thinking Node would work since I am already working with Javascript, as I understand it, it essentially allows JS to run on a server. I want to use MongoDB because it's easier to change the schema vs something like postgreSQL.

With Express you could do something like this when getting all the users (this code is from an api I made so it might not be exact):

router.get('/users', function(req, res, next) {
    const userData = {
        _id: false,
        password: false,
        about: false,
        __v: false
    }
    User.find({}, userData, function(err, user) {
        if(err) {
            res.send('Unable to find user');
            console.log(err);
        } else {
            res.json(user);
        }
    });
});

Obviously the main problem is that this relies on a user going to a route the executes this code, How can I make it so a Node app (or express if possible) can just sit on the server, I can run the file node app.js and then it would constantly run and add the response data to the database (which would be on the same server) without having to worry about going to any pages or otherwise making the code execute?

Please let me know if you have any questions, or would like more clarification.


Solution

  • Store the code you'd like to have run in a reusable regular function instead coupling it as an anonymous function with the route handler. Then, use a scheduler like node-cron or something similar which calls the function at intervals.