2

Trying to bring a local project onto an ubuntu mongodb server. Currently the project runs on my localhost:8000 when I run npm start on my server and I visit curl http://localhost:8000 i can see the markup of my homepage being outputted. How can i change this to use my domain/server ip in production?

Below is my node file which is run by npm start

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('05-express-first-app:server');
var http = require('http');

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

var port = normalizePort(process.env.PORT || '8000');
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);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

My server ip address is: 165.227.196.209, just not sure where to put it.

Thanks!

h0bb5
  • 609
  • 1
  • 7
  • 22

1 Answers1

1

You'll have to change the IP on which the Node.js server runs to be whatever the public IP address of your server is. In your case, the public IP is 165.227.196.209

This is assuming your name server is already configured to route the domain (eg: mywebsite.com) to that particular IP address.

Read more about it here - How to assign a domain name to node.js server? (Check the selected answer)

UPDATE:: Opening the URL - http://165.227.196.209/, I notice that you're using nginx. So you'll have to setup reverse proxying.

I suggest going through the link here - Node.js + Nginx - What now?

You can read about the benefits of putting nginx infornt of Node.js here - Using Node.js only vs. using Node.js with Apache/Nginx

Couple of other problems that I notice here that would be good to fix,

  1. You should be using a process manager to run your application in Production. Something like pm2 is recommended. This way if you application crashes, pm2 will restart it.
  2. You can use pm2 with environment variables as described here to run on a different port on production - http://pm2.keymetrics.io/docs/usage/environment/
Abijeet Patro
  • 2,842
  • 4
  • 37
  • 64
  • one should be able to just use IP address without worrying about the domain name. also it should work without PM2. I think the problem is something else. – Aᴍɪʀ Jan 30 '18 at 18:38
  • 1
    @Aᴍɪʀ - Updated, he is running the server behind nginx, so he has to setup a reverse proxy. – Abijeet Patro Jan 30 '18 at 18:41
  • BUT, nginx only uses port 80! his node app uses port 8000 by default, it shouldn't be a problem there either! – Aᴍɪʀ Jan 30 '18 at 18:42
  • 1
    @Aᴍɪʀ - Look at his comment, he says " I have done that. When I run npm start the server boots up normally and I can visit curl http://localhost:8000 and see the get request from my server. However in a web browser I cannot view my site and it is not being hit with my domain" . Entering his domain will obviously route to nginx, which doesn't know anything about the Node.js server running in the background. – Abijeet Patro Jan 30 '18 at 18:45
  • I think he's missing something else! :shrug: – Aᴍɪʀ Jan 30 '18 at 18:55