Our announcement about supporting new languages generated enthusiasm, and several customers are already running Node.js applications. We’re therefore keeping our momentum up in this post, which focuses, as you might expect, on Node.js.
A major language
As promised last week, we have a surprise: Node.js is becoming the fourth major language that alwaysdata will be supporting, after PHP, Python, and Ruby. What does this mean?
In a certain way, you could say that alwaysdata has supported Node.js forever: “all” you had to do was to download the interpreter from the official website into your account to make Node.js run. On the web side, though, you had to play it clever in order to run the application, for example by using PHP in reverse proxy. Doable… but complicated.
That it’s becoming a major language implies:
- that we’ve pre – installed the interpreter, and that it is even possible to choose from among several versions of the interpreter (via the Environment menu in alwaysdata’s administration). Today, Node.js is available in versions 6.x (6.11.2) and 8.x (8.4.0)
- that new versions of the interpreter will be regularly installed, not just security updates
- that it will be easy to launch a web application in this language
- that it will be easy to install external dependencies, by using the official package manager in SSH. For Node.js, that’s npm, which is also pre-installed
We’ve thus added a new type of website, Node.js. It’s similar to the User program type – since Node.js applications integrate their own HTTP server and don’t require an intermediary layer – with one main difference: it’s possible to specify an explicit version of Node.js for this site. And therefore to have several distinct websites using different versions of Node.js.
Example: deploying an Express application
Express is one of the best-known Node.js frameworks. Let’s see how to host an application developed using it.
- First off, we have to install Express. Follow the official documentation, executing the commands in SSH (and accepting everything by default):
1 2 3 4 | $ mkdir myapp $ cd myapp $ npm init $ npm install express --save |
- Then, let’s create the app.js file, all the while continuing to follow Express’s documentation. Modify the code slightly so that the application is listening on the IP and port that will be provided as environment variables:
1 2 3 4 5 6 7 8 9 10 | const express = require('express') const app = express() app.get('/', function (req, res) { res.send('Hello World!') }) app.listen(process.env.PORT, process.env.IP, function () { console.log('Example app started!') }) |
- Finally, from within alwaysdata’s administration, let’s create a Node.js type site, inserting the following command:
1 | node ~/myapp/app.js |
There you have it: our little Express application can indeed be reached!
Next language coming up, for the second post in our series: Lua.
Wow, well that was ridiculously easy! To install a nuxt application, all I had to do was add the required host and port to the ‘environment variables’, set the working directory to the root of the application, and set the command to ‘npm run start’.
Will Elixir be this easy to install?
Darren: well, it’s actually even easier than what you did: environment variables (ALWAYSDATA_HTTPD_PORT and ALWAYSDATA_HTTPD_IP) are already automatically defined for you, you don’t need to declare them explicitely!
Regarding Elixir, yes it should be very similar.
For my username, such variables seems to be undefined, since this program prints “undefined” when it’s started. Moreover, it seems that such variable does not appear on printenv
const express = require(‘express’)
const app = express()
app.get(‘/’, function (req, res) {
res.send(“Hello World”);
})
app.listen(process.env.ALWAYSDATA_HTTPD_PORT, process.env.ALWAYSDATA_HTTPD_IP, function () {
console.log(process.env.ALWAYSDATA_HTTPD_PORT);
})
Jackbergus: those variables are only defined when your application runs on the HTTP server (after you’ve created a site in the Web > Sites section), not in SSH (which you seem to use as you mention printenv).
Thank you for adding this feature!
i got to put myapp folder where in www ?
Larade: can you open a support ticket if you need help?