Node.js example scripts

 

Overview

This article lists a few simple Node.js examples you can use to test your installation.

Running Node.js on ports 80 or 443

While it's not possible to assign your app to use ports 80 or 443, you can configure a Proxy Server in the panel. This allows your app to use a non-privileged port in combination with your SSL certificate.

Testing if Node.js is working

The following examples are quick ways to test if Node.js is functioning.

Test with console.log

Create and run this script within your terminal to determine if Node.js was installed correctly.

  1. Create a file named helloworld.js with the following line:
    console.log('Hello World!');
  2. Run the script using the node command.
    [server]$ node HelloWorld.js
    Hello World!
    

    If Node.js is correctly installed, you will see the console respond with Hello World!

Test a script

You can also create a basic HTTP server to load a site. This HTTP server will listen on port 8888.

  1. Create a file in your application directory named app.js with the following code:
    var http = require("http");
    
    http.createServer(function(request, response) {
      response.writeHead(200, {"Content-Type": "text/plain"});
      response.write("Hello World. This page is running Node.js version: ");
      response.write(process.version);
      response.end();
    }).listen(8888);
    
  2. Run the node command to start the server:
    [server]$ node app.js
  3. Visit the page in a browser with port 8888 appended to the end. For example:
    • http://example.com:8888

    Viewing your domain with a port bound to it only functions for VPS and Dedicated Servers.

    If running correctly, your site will display the following:
    Hello World. This page is running Node.js version: v###

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?