1. Home
  2. Guides
  3. Running Node.js

Running Node.js

Overview

Node.js is a performant JavaScript backend built off Chrome’s JavaScript engine (v8). It’s also wicked fast. Node.js and its accompanying package management, npm, are available on newer platforms (v6+) without any additional compilation from source. Accounts with terminal access are eligible to use Node.js and npm.

Running Node.js with Passenger

Newer hosting servers, v6+ and above, support running Node.js through Passenger. Passenger automatically manages launching Node.js and scaling the number of Node.js instances to meet demand. To adapt a Node.js script to Passenger, create a compatible filesystem layout:

nodejsapp
+-- app.js  <-- main file
+-- public  <-- document root
¦   +-- .htaccess <-- htaccess control file
+-- tmp     <-- passenger control/scratch directory

Create a .htaccess file in public/, which serves as the document root, with the following lines:

PassengerNodejs /usr/bin/node

Note (v6.5+ platforms): if the system version is insufficient, use nvm to specify or install a different Node interpreter. When specifying the path to PassengerNodejs, be sure to expand the tilde (~) to your home directory.

Note: (v6 platforms) if the system version is insufficient, you may use your own Node.js version installed under /usr/local/bin. Change PassengerNodejs from /usr/bin/node to /usr/local/bin/node.

Next, rename the main file to app.js and locate this under public/ as in the directory layout. Connect the public/ folder to a subdomain or domain within the control panel and you’re all set. You can specify another entry-point via the PassengerStartupFile directive.

You can restart Node.js using the same restart mechanism as with Ruby or Python scripts.

Specifying another startup

In the .htaccess file, specify: PassengerStartupFile newfile.js where newfile.js is the location to another file not named app.js.

Running Node.js standalone

Quickstart

The following lines of code should be added to a file called server.js. Replace 40201 with a port preallocated to your account.

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
 response.writeHead(200, {"Content-Type": "text/plain"});
 response.end("Hello Worldn");
});

// Listen on port 40201, pre-allocated , IP defaults to 127.0.0.1
server.listen(40201);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:40201/");

A quick and easy way to do this is with Vim, a text-editor available through the terminal:

  1. vim ~/myserver.js
  2. Type i on the keyboard to switch to “Insert” mode
    • Depending upon client, paste the text through CTRL + V, Shift + INS, or a suitable key combination
  3. Hit the Esc(ape) key.
  4. Type :wq
  5. Done!

Now to start Node.js using the above server script, type: node ~/server.js:

[myuser ~]$ node server.js
Server running at http://127.0.0.1:40201/

Congratulations! Your Node.js server is running. You can send a simple request using curl with curl http://127.0.0.1:40201/ to confirm everything is working:

[myuser ~]$ curl http://127.0.0.1:40201
Hello World!

Persisting a server

Use forever through npm (npm install -g forever) or nohup to run keep a server running even after logging out: nohup node server.js &

Starting on Start-up

  1. Visit DevTask Scheduler within the control panel to schedule a new task.
  2. Under Command, enter node ~/server.js
  3. Under Scheduling, select Server Start
  4. Click Add

Installing packages

Use npm to install packages. Syntax is of the form npm install -g PKGNAME where PKGNAME is a package listed through npm.

Configuring global install on older platforms

Platforms older than v6 will require a .npmrc file present within the home directory to define 2 variables, prefix and link. These 2 variables will set the location in which binaries are installed and make a link so the binary appears in your shell path:

prefix = /usr/local
link = yes

Once this file is present in your home directory with those 2 lines, then node install -g will properly install packages under /usr/local instead of within the current working directory.

See also

Updated on July 8, 2019

Was this article helpful?

Related Articles