A CLI command to start up a node server, execute another CLI command (while node server is still running) and then exit the node server.
Why on earth would you want that?
I created node-while for my specific use case - I wanted to start up my node server, run my integration tests and then stop the server (all via a single CLI command). This seemed like a reasonable way to do it + I thought it might be useful for someone else.
Install
npm install node-while
Usage
To be used with node-while, your node server needs to do 2 things;
- export the express app instance as default
- emit the event 'ready' when the server has finished init (e.g. connected to DB) and is ready to recieve requests
e.g.
//For ES6;const app = ; const server = app; //1. export the app instance as default;
//or for vanilla ES5var express = ;var app = ; var server = app; //1. export the app instance as defaultexportsdefault = server;
You can use node-while like so
npx node-while -server ./src/myServer.js -run "echo squanch"
(concrete example) In my particular case I use node-while to run my cypress integration tests
npx node-while -s ./dist/server/index.js -run "npx cypress run"
See node-while -h
for addition params
Help, my server code uses babel/typscript
node-while doesnt currently have hooks for transpilers etc. Current recommendation is to transpile code e.g. with babel CLI (as your probably doing for production anyway) and then execute that with node-while.