Can I tell foreman to reload the web app every time a request is made so I can develop decently?

后端 未结 6 708
南方客
南方客 2020-12-24 13:18

A web app I am writing in JavaScript using node.js. I use Foreman, but I don\'t want to manually restart the server every time I change my code. Can I tell Foreman to reload

6条回答
  •  眼角桃花
    2020-12-24 14:12

    The problem isn't with Foreman so much as it's with how node doesn't reload code on new requests. The solution is to use an npm package like supervisor along with an environment wrapper for Foreman.

    First, install supervisor:

    npm install -g supervisor
    

    Then, write a wrapper shell script that Foreman can call:

    if [ "$NODE_ENV" == "production" ]; then
      node /path/to/app.js
    else
      supervisor /path/to/app.js
    fi
    

    Set the wrapper script's permissions to executable by running chmod a+x /path/to/wrapper_script.sh

    Lastly, update foreman to use the wrapper script. So in your Procfile:

    web: /path/to/wrapper_script.sh
    

    Now when you run Foreman and your node app isn't running in production, it should reload on every request.

提交回复
热议问题