Node.js Guide
Overview
What is Node.js?
Node.js is a performant JavaScript backend built off Chrome's JavaScript engine "V8". Node.js and its accompanying package management, npm, are available without any additional compilation from source.
Running Node.js with Passenger
KT supports 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 line:
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 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
.
Standalone Node.js
Quickstart
The following lines of code should be added to a file called server.js
. Replace 40201
with an alloced port (See Account > Info) from 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 World\n");
});
// 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 Nano Text Editor, which is available through the terminal:
nano ~/myserver.js
- Paste copied code using CTRL + V or CMD + V keys
- Use CTRL + X or CMD + X to save file
- Press Y to accept saving
- 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!\
Installing Packages
Use NPM to install packages. Syntax is of the form npm install -g [Package Name]
where [Package Name] is a package listed through npm.
Persisting a server
Use forever (npm install -g forever
) or nohup command to run keep a server running even after logging out: nohup node server.js
Starting on Start-up
- Visit Dev > Task Scheduler within Control Panel to schedule a new task.
- Under Command, enter
node ~/server.js
- Under Scheduling, select Server Start
- Click Add