简体   繁体   中英

Express.js middleware and bootstrap application

I'm currently working on a nodeJS app that's need to check some hardware and software requirements at bootstrap before starting to expose an express API.

The issue that I'm facing with express is that it need a HTTP request to trigger an action, however my bootstrap step do not require a request as it is an autonomous server side routine executed at each start of the app.

For now, I just made my bootstrap process a node module, require it and execute it before calling the express app instantiation. However, I feel this way not that clean.

If anyone can help, it would be wonderful :D

Because the javascript nature, you can always run async task before doing something.

// checlForMinimalRequirements.js
function checkForMinimalRequirements() {
  return new Promise((resolve, reject) {

    // this is just an async mock
    setTimeout(() => {
      return Math.random() > 0.5 ? resolve() : reject();
    }, 2000);
  });
}


// index.js

import express from 'express';
import {checkForMinimalRequirements} from 'checkForMinimalRequirements';

Promise
  .resolve(checkForMinimalRequirements())
  .catch(() => {
    console.error('Missing Minimal Requirements');
    process.exit(1);
  })
  .then(() => {
    const app = express();

    return express.listen(/**/)
  })
;

For now, I just made my bootstrap process a node module, require it and execute it before calling the express app instantiation. However, I feel this way not that clean.

Why shouldn't it be clean? In fact, this is very clean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM