title: Node.JS Application - Tasks and API
body: <p>General question. </p>
  
  <p>I am looking to create a console-like application with Node.js that also has api capabilities. It will have timed task that will run on a schedule but also be available to send http request to. My problem is that while I've created the api. I don't know how to go about creating basic task to be performed on a schedule while the server stays running in case http request is made for different types of task. </p>
  
  <p><strong>Main idea is to have the task schedule to run while also keeping the server running and waiting for http request</strong></p>
  
  <p><strong>Folder paths</strong></p>
  
  <blockquote>
    <p><em>folders</em><br/>
    -controllers<br/>
    -models<br/>
    -modules<br/>
    -node-modules<br/>
    -routes<br/>
    <em>general files</em><br/>
    app.js<br/>
    package-lock.json<br/>
    package.json<br/>
    server.js
    task.js</p>
  </blockquote>
  
  <p><br />
  <em>Proposed area for Scheduled task to be performed:</em><br/><br/>
  <strong>index.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>    const { checkTablesForData, requestRoutine, runNecessaryUpdate }  = require('./task')
  
      function Main() {
          ...
          \\task to be executed
  
          \\Check the table for records to be processed
          let obj = checkTablesForData();
  
          \\Send third party request with object data. ie - id's
          obj.map( id =&gt; {
              \\call request routine
              requestRoutine(id)
          })
  
          \\Run final process
           runNecessaryUpdate(id)
      }
  </code></pre>
  
  <p><strong>task.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>
      checkTablesForData(){
          ...
      }
  
      requestRoutine(id){
          ...
      }
  
      runNecessaryUpdate(id){
          ...
      }
  
      module.exports = { runNecessaryUpdate, checkTablesForData, requestRoutine } 
  </code></pre>
  
  <p><br />
  <em>Code for api setup</em>
  <br/></p>
  
  <p><strong>app.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>    const express = require('express')
      const path = require('path')
      const app = express() //, api = express();
      const cors = require('cors');
      const bodyParser = require('body-parser');
  
      app.get('/', function(req, res){
          res.redirect('./api')
      })
  
      const api = require('./routes/api');
      app.use('/api', api)
  
      module.exports = app 
  </code></pre>
  
  <p><strong>server.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>    const app = require('./app');
  
      const port = process.env.PORT  || 3000;
  
      app.listen(port, 'localhost', () =&gt; {
          console.log(`Server is running on port ${port}`);
  
      });
  </code></pre>
  
  <p><strong>api.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>
      const express = require('express');
      const router = express.Router();
  
      //Controller Modules
      const controller = require('../controllers/homeController');
  
      //Routes
      router.get('/request/:id', controller.post)
  
  
  
      module.exports = router;
  </code></pre>
  
  <p><strong>controller.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>
      //Send request to 3rd party api
      export.post = function(req, res){
          const options = {
              ....
          }
  
          return request(options)
          .then( response =&gt; {
              ...
          }
          .catch( error =&gt; {
              \\error routine
          }
      }
  </code></pre>
  
  <p>Advice is very much needed. </p>
  
  <p>Thanks!</p>