代码之家  ›  专栏  ›  技术社区  ›  Dem Pilafian

从npm脚本在后台运行http服务器

  •  28
  • Dem Pilafian  · 技术社区  · 7 年前

    你是怎么开始的 http-server 从一个 npm公司 脚本使另一个 npm公司 脚本,例如 Mocha test using jsdom ,可以向发出HTTP请求 http服务器 ?

    这个 http服务器 安装程序包时包含:

    npm install http-server --save-dev
    

    这个 package.json 文件包含:

    "scripts": {
       "pretest": "gulp build-httpdocs",
       "test": "http-server -p 7777 httpdocs/ && mocha spec.js"
    },
    

    跑步 npm test 成功启动 http服务器 ,但命令当然会在显示以下内容后挂起:

    Starting up http-server, serving httpdocs/
    Available on:
      http://127.0.0.1:7777
      http://192.168.1.64:7777
    Hit CTRL-C to stop the server
    

    是否有一种简单的方法来启动web服务器,使其不会阻止Mocha测试?

    奖金: 如何关闭 http服务器 在摩卡咖啡测试运行之后?

    2 回复  |  直到 3 年前
        1
  •  29
  •   Alex Michailidis    6 年前

    您可以在后台通过附加 & 最后 然后使用 postscript npm提供给我们的钩子,以终止后台进程。

    "scripts": {
        "web-server": "http-server -p 7777 httpdocs &",
        "pretest": "gulp build-httpdocs && npm run web-server",
        "test": "mocha spec.js",
        "posttest": "pkill -f http-server"
    }
    

    但如果我有多个 http-server 跑步

    您可以通过在 posttest 脚本:

        "posttest": "kill $(lsof -t -i:7777)"
    

    现在对于Windows,语法不同了,据我所知,npm不支持多个操作系统脚本。为了支持多个操作系统,我的最佳选择将是一个吞咽任务,它将处理每个不同的操作系统。

        2
  •  2
  •   Camila Belo    4 年前

    跨平台问题可能可以通过以下方式解决 npm-run-all