【问题标题】:Prevent Nodemon to restart some specific code防止 Nodemon 重启某些特定代码
【发布时间】:2021-11-27 22:52:59
【问题描述】:

我在我的nodejs 项目中使用nodemon,因为我希望每当我进行任何更改时它都会自动重新启动一切正常,但现在问题是我想使用一个库 其中包括 puppeteer lib 每当我进行任何更改时 nodemon 关闭铬浏览器并重新打开它需要一些时间。这使我发展缓慢。有什么办法可以阻止这种行为。

这是我的代码。

const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Client } = require("whatsapp-web.js");

const client = new Client({ puppeteer: { headless: false } });
client.initialize();

console.log("changes 7");

server.listen(3000, () => {
console.log("listening on *:3000");
});

每当我进行任何更改时,它都会重新启动一切。我不想每次都重启client

【问题讨论】:

    标签: node.js puppeteer nodemon


    【解决方案1】:

    我不知道nodemon,但如果你可以编辑库,你可以重新使用现有的浏览器。

    尝试,从节点外壳:

    (await require('puppeteer').launch()).wsEndpoint()
    

    这会返回一个您可以重复使用的连接字符串。

    然后,您可以使用connect api 连接到创建的实例

    编辑:您的库允许 ws 套接字! :-)

    const client = new Client({ 
        puppeteer: {
            browserWSEndpoint: `ws://localhost:3000`
        }
    });
    

    【讨论】:

      【解决方案2】:

      在你的项目目录中创建 nodemon.json nodemon 将自动查找此文件并在存在时使用它 并写在nodemon.json

      {
        //list of directory you want to watch
        "watch": ["./","server","someOtherDir"],
        "ext": "js,ts,json", //file extension to watch
        "ignore": ["ignoreThisDir","someDir/*.js"], //specify files or directory to ignore
         // specify entry of the project
         "exec" : "node app.js"
         //another example for exec "exec": "ts-node --project tsconfig.server.json server/app.ts"
      
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-20
        • 1970-01-01
        • 1970-01-01
        • 2019-10-20
        • 1970-01-01
        • 2022-11-24
        • 2018-06-04
        相关资源
        最近更新 更多