By kpennell
Hello all. Stuck trying to setup up several Node apps on one droplet. I followed the Host Multiple Node.js Applications On a Single VPS with nginx, forever, and crontab article exactly.
I have the domains all pointed correctly and A records set.
I can’t seem to get apps to run (with forever) on any other port besides the default express 3000.
I changed the server_names_hash_bucket_size 64; (like it says) I created /etc/nginx/conf.d/example.com.conf for the apps (they are different domains. I put 1 on port 3000 and the other on 4000)
I don’t understand the difference between when Nginx is running the app and when forever is? Where does “npm start” come into play? How many potential servers are working at the same time?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
check out [https://github.com/substack/bouncy] or [https://www.npmjs.com/package/bouncy] if you want to do it the NodeJs/Express way. assume port 8001 is node app A and 8002 is node app B. change to port 80 and nodemon this file.
var bouncy = require(‘bouncy’);
var server = bouncy(function (req, res, bounce) { if (req.headers.host === ‘beep.example.com’) { bounce(8001); } else if (req.headers.host === ‘boop.example.com’) { bounce(8002); } else { res.statusCode = 404; res.end(‘no such host’); } }); server.listen(80);
Nginx doesn’t run the app. It is a webserver and it acts as a proxy in this case. The apps are run on a non-standard port and Nginx than relays then to port 80 on the correct domains. <br> <br>Selecting the port to run on, is up to the app itself. Are these apps you’ve written or just things you’ve installed? Usually, you can set the port as an environment variable. So, you’d start your app with something like: <br> <br><pre> <br>PORT= 4000 forever start --sourceDir /path/to/your/node/app main.js <br></pre> <br> <br>That assumes that the app does something like the bellow. This is from the template created using express-generator, so it is fairly universal: <br> <br><pre> <br>#!/usr/bin/env node <br>var debug = require(‘debug’)(‘my-application’); <br>var app = require(‘…/app’); <br> <br>app.set(‘port’, process.env.PORT || 3000); <br> <br>var server = app.listen(app.get(‘port’), function() { <br> debug('Express server listening on port ’ + server.address().port); <br>}); <br></pre> <br> <br>That defaults to using port 3000 unless the environment variable is set.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.