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 without any additional compilation from source. Accounts with terminal access are eligible to use Node.js and npm.
Running Node.js with Passenger
Our hosting servers 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: 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.
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:
vim ~/myserver.js
- 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
- Hit the Esc(ape) key.
- Type
:wq
- Done!
Now to start Node.js using the above server script, type: node ~/server.js:
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:
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
- Visit Dev > Task Scheduler within the control panel to schedule a new task.
- Under Command, enter
node ~/server.js
- Under Scheduling, select Server Start
- 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.