hosting a web app

I have successfully followed the example. My understanding is that node.js is only required for development and in ‘production’ all that is left is static/clientside code. Is that correct? If not, how will/can nodejs be hosted on a distributed platform?

1 Like

I shared this confusion, but NodeJS is not just server side. It has been adapted so that now it can be used in the client (see browserify, but that is no longer the only route).

So these example apps using nodejs work fine without a server on SAFE Network.


TL;DR: The solution above is not quite accurate. In production, Node is not used or required during any step in the process.

Essentially, Node lets you execute JavaScript code on your computer without requiring a browser.

Websites – both on the regular web and on the SAFE Network – are composed of HTML (for structure), CSS (for style), and JavaScript (for instructions). Web browsers like Firefox or Peruse know how to interpret all those different files in order to display an interactive website.

In contrast to a web browser, Node is a “runtime” that can execute JavaScript code – just like the runtime for Java, Go, PHP, and other programming languages. Node can understand and execute JavaScript, and that’s it.

Node can be used in development to help you compile and serve your code for viewing on http://localhost. You can choose to use a tool like Browserify or Webpack to automatically turn the JavaScript you write into a fully functional HTML + CSS + JS website.

Once you compile your code for production, as you mentioned, all you’re left with is static HTML + CSS + JS files. Those static files are hosted by computers on the SAFE Network, and when you visit a SAFE website, your browser reads those static files and displays the website.

In production, Node is not used or required during any step in the process. It was simply used to compile the code when you originally published the website.

Side-note: You can also use Node to run a live web server, which is a completely separate scenario and not yet possible on the SAFE Network. Instead of using Node to compile a website, you could use Node to run a JavaScript application. This works in the exact same way as a PHP or .NET server, if you’re familiar with those.

1 Like

@cooperka, i don’t think this is correct:

From the article you linked:

Both your browser JavaScript and Node.js run on the V8 JavaScript runtime engine. This engine takes your JavaScript code and converts it into a faster machine code. Machine code is low level code which the computer can run without needing to first interpret it.

From what i understand the difference is that some code is purely browser clientside js (f.e. alert) and some node serverside (f.e. http server)

From Node’s homepage:

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.

Node is both written in JavaScript, and used to execute JavaScript. It uses Google’s open-source V8 engine internally.

Does that help clarify?

The code itself is not browser vs. server; it’s a matter of what facilities are available in the execution environment. Most terminals don’t provide an alert function, so Node will crash if you try to call alert because it simply doesn’t exist. Most browsers, however, provide that function so it will work.

For fun, you can use a library such as alert-node to get alert in Node.

Similarly, running an HTTP server isn’t feasible from inside a browser. The code is perfectly valid, but the browser doesn’t provide the required facilities. Those are only available in the terminal via Node.