forio Toggle navigation

Node.js

Node.js is JavaScript coding that you want to perform on the server-side, rather than in your client-side UI. This feature is only available for Enterprise teams.

The current version of Node.js in Epicenter is v6.11.1.

It's easiest to see how Node.js works by looking at an example, like the one below. If you are familiar with Node already, you can simply enable Node.js in your project's settings.

Epicenter takes care of all the server-side setup, including creating and sandboxing your Node server for you.

In the example, we'll first create a project composed entirely of client-side code. Then, we'll replace that code with server-side code. Finally, we'll look how you might adapt this to a real-life scenario.

Step 1: Create a simple project

First, create a simple team project. (Node.js is only available for Enterprise teams.)

  1. From your Dashboard, find your team and select Create a New Project. Enter the Project Name and Project ID, then choose Create Project.

  2. Optionally, create a basic model. You won't actually need to reference any model variables in order to enable Node.js and see it working, but you can create or upload a file here if you like.

  3. Create a basic user interface for your project. Select Interface and choose to Add File from the Actions menu. Name the new file index.html. The purpose of this file is simply to confirm that your project is running static files (as opposed to Node.js), so it can be very simple:

     <!-- index.html in the Interface section of your project -->
     <html>
     <body>
         <div>Hello world from index.html</div>
     </body>
     </html>

    This content is sent directly to the client web browser and executed there: index.html is rendered when you view the project.

Step 2: View your project

View your project by visiting forio.com/app/[your-team-id]/[your-project-id] in a web browser. For example, forio.com/app/acme/hello_world/.

Step 3: Replace with server-side code

Next, change your project so that its code is executed on the server-side, and add appropriate server-side code. Epicenter projects have the option to use Node (Node.js) as the server for this purpose.

Enabling the Node server

To enable a Node server for this project:

  1. From your project home page, select Settings from the left.
  2. Select the Node Settings tab.
  3. In the Server setting, select Enabled. Then choose Save Node.js Settings.

Important: Notice that once you enable the Node server, you have an additional project setting, Code Cache. Use this to reset (clear) the cache on the Node server. The cache is cleared automatically when you edit, add, or remove files in the project's Node.js section, but you can also clear it manually here.

(See more details on project settings.)

Enabling the Node.js server immediately does two things:

  • It enables the Node.js section in your project, available from the left sidebar.
  • It sets up your own, sandboxed, Node server. The current version of Node.js in Epicenter is 0.10.28. Your server-side code can start up this server whenever an end user interacts with your project, and it will serve the files located in the Node.js section of your project INSTEAD OF the files located in the Interface section of your project.

Adding server-side code

When you first enable the Node server for a project, a simple index.js file is automatically created in the Node.js section of your project as an example. For Epicenter projects, the Node server looks for a file in the Node.js section called index.js and starts executing from there. You can put additional files here by adding or uploading them.

Let's take a look at this sample file:

var http = require('http');

var server = http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Node.js is working!');
});

server.listen(80);

This example starts the Node server for your project, then has the server write some text. This text is generated on the server-side, and the client only sees and renders this text.

Of course, you can modify this example to have the server write html instead of text by updating the Content-Type and changing the body of the response:

var http = require('http');

var server = http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end('<html><body><p>Node.js is working!</p> <p>on an html page</p></body></html>');
});

server.listen(80);

Step 4: View your project

View your project again by visiting forio.com/app/[your-team-id]/[your-project-id] in a web browser. For example, forio.com/app/acme/hello_world/.

Notice that even though you're visiting the same URL, and even though you have not changed any of the content in your Interface section, the project appears differently to the end user. The content is now being served based on the files in the Node.js section of your project. The code in these files is executed on the server and the results are passed directly to the client.

Next Steps

Obviously, this is a very basic example.

In practice, enabling Node for a project is often used for increasing performance and improving the maintainability of your code. For example, you might aggregate API calls, perform validations, or execute other repeated tasks on the server-side.

The complete details of working with Node are documented at Node.js.