node.js - 如何从 Node 文件创建命令行应用程序

标签 node.js

我在 npm 包中有以下模块,我想以某种方式启动它并保持事件状态,直到发出停止命令。这是一个使用 ldapjs 的假 LDAP 服务器:

const ldapjs = require('ldapjs');
var server;

const start = (config, data, cb) => {
  baseDN = config.baseDN;
  db = data;

  if(server) {
    if(typeof cb === 'function') {
      return cb();
    }

    return;
  }

  server = ldap.createServer();

  // rest of code

  server.listen(config.port, () => {
    if (typeof cb === 'function') {
      return cb();
    }
  });
};

const close = (next) => {
  if (server) {
    server.close();
  }

  server = null;

  if (typeof next === 'function') {
    return next();
  }

  return;
};

module.exports = {
  start: start,
  close: close
};

如何制作一个从命令行调用的可执行文件?我需要该过程无限期地运行,并且我在工作中可以安装的内容受到限制。例如,我无法永远安装。

此外,导出此内容的最佳方式是什么?我应该使用 npm 的 bin 部分吗?

最佳答案

使用其中之一:

永远 - https://www.npmjs.com/package/forever

forever start app.js

forever list

forever stop app.js

指挥官 - https://www.npmjs.com/package/commander

mycli -options

关于node.js - 如何从 Node 文件创建命令行应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42082944/

相关文章:

javascript - 在执行下一步之前等待函数完成 - Node js

javascript - 将node.js脚本的进程从node-default重命名为

Node.js Mongoose 因复杂查询而变得困惑

node.js - 使用 npm 安装 Express 时出现问题。

node.js - NodeJS 异步函数创建页面加载器

javascript - 分隔 .CSV 文件中的数字

node.js - 更新期间省略集合中具有 null 的元素

javascript - 数组中带有权重的随机数

node.js - 为什么 Node.js 具有可扩展性?

node.js - 在 Mongoose 中更改 pre ('validate' ) 中间件中的字段是否正确?