3

In my live server I have my expressjs app running at

http://122.22.11.22:3000

But how can I use a domain name or sub domain name instead? for instace:

http://my-express.com

or

http://exress.mymastersite.com

I use express-generator to create my express skeleton btw. And I am on Linux Arch.

Any ideas?

Inside bin/www I have this code below generated by the express-generator automatically by default:

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('mongoose-iot:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

....
....
....

1 Answer 1

4

What you want to do is called virtualhosting, You can use nginx to do it

Make your express listens only on localhost : server.listen(3000, 'localhost');

And setup your ngnix as this :

server {
    listen 80;

    server_name express.your-domain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    } }

You can add as many virtualhost as you want.

2
  • any alternative to ngnix? I have a problem installing it on my localhost ubuntu so i dare not install it in my live server arch linux.
    – Run
    Sep 28, 2016 at 10:03
  • 1
    apache or haproxy can do it as well. Or you can try github.com/expressjs/vhost directly in nodejs. Sep 29, 2016 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.