Running Node Scripts - abbernie/web-dev-sp17 GitHub Wiki
Deploying to Digital Ocean
When you're ready to put your project online, connect to Digital Ocean using FTP, and upload your code -- don't forget to include the node_modules
folder and the package.json
file.
It's a good idea to put each project in a new folder, since all your projects will have some files and folders with the same name. You can create a new folder in Cyberduck by going to File > New Folder in the menu bar.
Once you've uploaded your project, log on to your Digital Ocean server using Terminal. Navigate to the folder where your javascript file resides and run your file in node with: node NAMEOFYOURFILEHERE.js
Now, to make sure your code is working, go to your ip address in a browser, making sure to add the port number. So for example, assuming your code uses port 3000
and your ip address is 12.345.67.89
, you'd go to 12.345.67.89:3000
in the browser.
Keep It Running
The default behavior of Node is to kill the script when you quit Terminal. That's fine for short-term use, but to keep your web site up and persistent, install Forever. You can use npm
to install forever. Log into your Digital Ocean server in Terminal, and type:
npm install forever -g
When you're ready to use forever
, you'll run your script using the forever
command instead of node
. Forever runs the node script for you. So, instead of:
node server.js
use:
forever start server.js
When you need to stop a script, use stop
instead of start
. If you're not sure if forever is running any scripts, you can ask for a list of currently running scripts:
forever list
More info on how the forever program works: http://www.hacksparrow.com/keep-node-js-script-running-after-logging-out-from-shell.html