What are Node.js and npm?
Node.js lets you run JavaScript on a server instead of only in a browser. npm is the tool that downloads packages for Node.js. A package is a bundle of ready-made code. To deploy means to move your app to a server and start it so everyone can use it.
Some shared hosting plans have a Node.js tool in the control panel. Ask Hostvento support whether your plan has one.
Steps
- Log in to your server with SSH. SSH is a safe way to type commands on a server from far away.
- Check that Node.js and npm are installed.
If you see "command not found", install them with your system's package tool.node -v npm -v - Copy your app to the server. You can use Git or an upload tool like SFTP.
Use your own repository address.git clone https://example.com/your/repo.git myapp cd myapp - Install the packages your app needs.
This readsnpm install --omit=devpackage.jsonand downloads everything in it. The option skips tools used only for development. - Test the app.
This runs the start script fromnpm startpackage.json. Stop it withCtrl+Cafter you see it works. - Install PM2. PM2 is a tool that keeps your app running and restarts it if it crashes.
sudo npm install -g pm2 - Start your app with PM2.
pm2 start npm --name myapp -- start - Make it start again after a reboot.
Run the command thatpm2 save pm2 startuppm2 startupprints.
Make it reachable
Your app listens on a port, such as 3000. A port is like a numbered door. Visitors expect the normal web doors, 80 and 443. Set up a reverse proxy with Nginx or Apache to pass traffic from those doors to your app. Also open the right ports in your firewall.
Keep it safe
- Keep secrets in an
.envfile. Never upload that file to a public repository. - Take a backup before you update.
- Run
pm2 logs myappto read errors.
npm install, then pm2 restart myapp.Quick recap
- Copy your app, run
npm install, test withnpm start. - Use PM2 to keep it running.
- Use a reverse proxy to serve it on ports 80 and 443.