Did you know we also include ready-to-go Node JS support? In fact, it only takes few quick steps to get a Node JS Express app set up. All in less than a minute! Read here to find out how.
Angelo Dini
Information Architect
Let's begin! Start by creating a new application on the Control Panel. Choose a name and be sure to select :
Stack: Node
Additional Components: Node
Additional Boilerplate: Default
You can also set up an Express project directly by selecting "Express" under "Additional Components". However, we will start from a blank canvas to build everything ourselves. This process should help you to set up any frontend project on Divio Cloud.
Press "Continue" and "Subscribe" on the subsequent screen when ready. You will start with a free Developer subscription to explore the product.
Divio Cloud will provision your project automatically. Hit "Deploy" on the test server to spin up an instance. Wait a bit until the "Env URL" is clickable. Click it to see your "Hello World!" application.
To get started, you'll need the Divio cli. Follow the installation instructions and head back to the Control Panel. Under "Commands," you will find the "Setup" tab. Click and copy and paste the link to the terminal on your computer. Wait for the process to finish.
Navigate to the Applications folder on your system and open up the code in your favourite editor. The project itself has a simple package.json
, Dockerfile
and index.js
already set up for you. The docker-compose.yml
file is used for local development. Browse the files to get a feel for it.
There are two ways to move forward:
Use your local system to code.
Install node (18.13.0 as of writing this article, see the current version in the Dockerfile
)
Run all commands via npm run ...
on your local machine
Use Docker to code.
This will not affect your system and is the preferred way.
Run all commands via docker-compose run web npm run ...
We will be using Docker moving forward, so adapt your commands accordingly when necessary.
Go to your applications folder and install Express through:
docker-compose run web npm install express --save
Next, delete the "public" folder and replace the contents of the index.js
File with the following code:
const express = require("express");
const app = express();
const port = 80;
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
This ensures that your code is served through express, run the following command to test your changes locally:
docker-compose up
This spins up the local development server. Open a browser with the following URL: "http://localhost:8000/". You can continue with additional changes following the Express tutorial at this point. To get your code to the cloud, run the following commands:
docker-compose run git add .
docker-compose run web git commit -m "added express"
docker-compose run web git push
Navigate to the Control Panel and hit "Deploy" again on the test server. You can preview your "Hello World!" on the Env URL once the project has been deployed. Congratulations!
If you need help with your initial setup with Divio, try the getting started tutorial.
Node.js is a trademark of Joyent, Inc. and is used with its permission. We are not endorsed by or affiliated with Joyent.