// Tutorial //

How To Setup a Node.js Development Environment on a VPS for a Team

Published on October 8, 2013
Default avatar
By Artur Burtsev
Developer and author at DigitalOcean.
How To Setup a Node.js Development Environment on a VPS for a Team

Introduction


In this tutorial, we will setup a Node.js development environment, which allows you to include rapidly new team members into the development process of your Node.js applications. This method can also be applied if a developer wants to create several versions of an application simultaneously.

This method is based on Node.js interacting with Nginx over Unix sockets instead of ports. Let’s assume that you have your development versions of the application at login.dev.nodeapp.com. In addition, we will hold the sockets for every developer in the /tmp directory, like /tmp/login.dev.nodeapp.com.sock.

Requirements


You will need to have Nginx and Node.js installed. Additionally, we will assume that you already have a domain (for example nodeapp.com) linked to your VPS. Note: you should also setup wildcard CNAME record for your domain. There are already well written tutorials about these topics on DigitalOcean:

Setting Up Nginx


We should create a new Nginx configuration file /etc/nginx/sites-available/dev.nodeapp.com which contains:

server {

    listen 80; 

    server_name ~^(?<login>[a-z]+)\.dev\.nodeapp\.com$;

    location / {

        proxy_pass http://unix:/tmp/$login.dev.nodeapp.com.sock:$uri$is_args$args;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }   
}

Link this configuration to sites-enabled folder and restart Nginx:

ln -nfs /etc/nginx/sites-available/dev.nodeapp.com /etc/nginx/sites-enabled/dev.nodeapp.com
/etc/init.d/nginx restart

Now Nginx is ready to accept user requests and guide them to the developer’s app copy, depending on URL. For example:

http://ivan.dev.nodeapp.com -> /tmp/ivan.dev.nodeapp.com.sock
http://anna.dev.nodeapp.com -> /tmp/anna.dev.nodeapp.com.sock

Modifying Node.js App


We will use a minimal webserver example from the Node.js, but the same modifications can be applicable for any Node.js server (like express).

The thing is, we need to change the default port-listening behavior into socket-listening:

var fs = require('fs');
var http = require('http');

var mask = process.umask(0);
var socket = '/tmp/' + process.env.USER + '.dev.nodeapp.com.sock';

if (fs.existsSync(socket)) {
	fs.unlinkSync(socket);
}
    
http.createServer(function (req, res) {
	res.writeHead(200, {'Content-Type': 'text/plain'});
  	res.end('Hello World\n');
}).listen(socket, function() {
	if (mask) {
		process.umask(mask);
		mask = null;
	}
});

console.log('Server running at ' + socket);

Now you can run your application node app.js and access it on http://yourlogin.dev.nodeapp.com.

Note: when Node.js starts listening for sockets, it creates a specified file. But if the socket file already exists, Node.js will fail to start listening. So we should make sure we remove the socket from the previous run.

The other thing is, we should create a socket with full access for all, so Nginx will be able to use it. It’s fine for development, but might not be good for production.

And that’s it, congratulations! All you need now to introduce a new developer to create user on your VPS.

<div class=“author”>Submitted by: <a href=“https://github.com/artjock”>Artur Burtsev</a></div>


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

The other thing is, we should create a socket with full access for all, so Nginx will be able to use it. It’s fine for development, but might not be good for production.

Please make a more detailed description, so it easy to understand that call to process.umask(0) makes next calls to creating a file to have chmod 777 on them.

Thank you for this article, it is exactly what I was looking for! I will try setting this up now.

I’m still having the same problem as @mica.cardillo, even with the typo fixed. No nginx errors, server running fine, have the CNAME wildcard set up for my domain… the only clue I may be able to offer is that both [domain].com and dev.[domain].com show the “Welcome to nginx!” page while [user].dev.[domain].com just says webpage is not available. :(

I think the \ causes an error for folks. I think it may be a typo:

proxy_pass http://unix:/tmp/\$login.dev.nodeapp.com.sock:$uri$is_args$args;

should be:

proxy_pass http://unix:/tmp/$login.dev.nodeapp.com.sock:$uri$is_args$args;

@mica.cardillo: Check if you can find anything in nginx’s error logs: <pre>sudo tail /var/log/nginx/error.log</pre>

I think I must have missed something with the nginx config, although I’ve gone through it several times to make sure it matches the instructions. Server is successfully running at /tmp/myusername/dev.domain.com.sock. However, no luck in the browser. Any tips on what I can do to troubleshoot further?