post-get-service
Version:
simple way to create node http in memory server
36 lines (29 loc) • 852 B
JavaScript
// @ts-check
const { get_model_file_path } = require('./commons/utils');
const server_routes_app_builder = require('./server_routes');
/**
* @param {!{host: string; port: string|number; api: {path: string}[]}} json_model_or_path api model
*/
function run_service(json_model_or_path) {
return new Promise((res, rej) => {
const json_model = get_model_file_path(json_model_or_path);
const PORT = json_model.port || 8081;
const HOST = json_model.host || '0.0.0.0';
const { app } = server_routes_app_builder(json_model);
process.on('uncaughtException', err => {
rej(err);
});
app.listen({ port: Number(PORT), host: HOST }, function (err) {
if (err) {
rej(err);
return;
}
res({
stop: async () => app.close(),
});
});
});
}
module.exports = {
run_service,
};