Express
Back to home
On this page
Welcome to our quick-start guide on hosting Express on Upsun where we will demonstrate just how simple it is to host your Express projects on our PaaS. Follow the steps detailed below and you’ll have everything set up in no time.
Anything included in these guides applies not only to Express, but also to NextJS and Strapi.
If you’re new to Upsun, you might want to check out the philosophy of Upsun to get started on the best possible footing.
Tip
To get your Express project up and running as quickly as possible, experiment with the Upsun demo app before following this guide.
Before you begin
You need:
- Git. Git is the primary tool to manage everything your app needs to run. Push commits to deploy changes and control configuration through YAML files. These files describe your infrastructure, making it transparent and version-controlled.
- A Upsun account. If you don’t already have one, register for a trial account. You can sign up with an email address or an existing GitHub, Bitbucket, or Google account. If you choose one of these accounts, you can set a password for your Upsun account later.
- Required: the Upsun CLI. This lets you interact with your project from the command line. You can also do most things through the Web Console, but this guide focuses on using the CLI.
Create your local Express app
First things first, if you don’t have a local Express project, you need to create a new Express project locally by following the official Express installation guide.
Please refer to all the steps of the official Express installation guide for further details, but to sum it up, here are the 4 steps to create an Express app locally:
mkdir my-express-app
cd my-express-app
npm init
npm install express Initialize your Git repository
We need to initialize the local Git repository and commit local files, using the following command:
git init
git add package.json package-lock.json
git commit -m "Init Express application"We should also ignore adding node_modules folder to the Git repository. Use the following commands to do so:
echo "/node_modules" >> .gitignore
git add .gitignore && git commit -m "adding node_modules folder in .gitignore file" Add a Hello World route
Please create your first Express page.
To do so, create a new index.js file at the root of your project. It will contain a basic Hello world script:
const express = require('express')
const app = express()
var port = (process.env.PORT || '3000');
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})Then please commit your file using the following commands:
git add index.js
git commit -m "adding index.js" Create a new project
The next step is to create a project on Upsun. To do this, you can either use the Upsun CLI or the Upsun Console.
To create a new project with the Upsun CLI, use the following command and follow the prompt:
upsun project:createNote
When creating a new project using the Upsun CLI command project:create, you will be asked if you want to set the local remote to your new project. Enter Yes (y).
Your local source code will be automatically linked to your newly created Upsun project by creating a .upsun/local/project.yaml file that will contain the corresponding <projectId> and set a Git remote to upsun.
git remote
upsunIf not, please refer to Set project remote section.
Create a new project from scratch.
If you do not already have an organization created to put the project in, you’ll first be instructed to create one.
Once you have done so, select that organization from the dropdown, and select Create from scratch.
In the form, fill in details like the project name and region. You’ll be able to define resources for the project after your first push.
After creating a project with the Console, you need to let the Upsun CLI know which linked project you want to deploy to.
To do so, use the Upsun CLI to set a remote project:
upsun project:set-remote <projectId>This command will add a new remote called upsun to your local Git repo as you can see below:
git remote
origin
upsunIt will also create a new .upsun/local/project.yaml file that will contain the given <projectId>, to store this information for the Upsun CLI interaction.
Note
If you don’t remember your <projectId> from the previous steps, you can get it back using this command line and select the one you created:
upsun project:list Choose your Git workflow
Upsun projects can be used as a classic Git repository where you will be able to push your source code in different ways, using either the Git CLI or the Upsun CLI. You can choose which way—or Git workflow— you would like to use for your project from the following options:
- Your project source code will be hosted on a Upsun Git repository
- Your project source code will be hosted on your own GitHub repository
git add . && git commit -m "message" && git push upsun) to commit your source code changes to Git history, and use the Upsun CLI to deploy your Upsun environment with the latest code updates.
Upsun provides a feature called Github integration that allows your Upsun project to be fully integrated with your Github repository.
This enables you, as a developer, to use a normal Git workflow (git add . && git commit -m "message" && git push) to deploy your environment—with no need to connect to the Upsun Console.
Note
Please make sure you that you have already completed the following steps before adding a Github integration:
-
Create a Git repository in your own organization following the relevant Github repository creation guide.
-
Create a Github integration.
-
Add a Git remote to your local project, from the root of your Express directory, by inputting the following:
Terminalgit remote add origin <urlOfYourOwnGitHubRepo> git add . && git commit -m "init express" git push origin
Configure your project
To be able to host your Express application on Upsun, some YAML configuration files are needed at the root of your project to manage the way your application will behave.
These YAML configuration files are located into a .upsun/ folder at the root of your source code, the architecture of which will look like this:
my-express-app
├── .upsun
│ └── config.yaml
├── [.environment]
└── <project sources>
Note
An additional .environment file could be located at the root of your source code, this file would contain Upsun specific environment variables.
To pre-generate these YAML files, please use the following command from the root of your Express project and follow the prompts:
upsun project:init
Welcome to Upsun!
Let's get started with a few questions.
We need to know a bit more about your project. This will only take a minute!
✓ Detected stack: Express
✓ Detected runtime: JavaScript/Node.js
✓ Detected dependency managers: Npm
Tell us your project name: [app]
(\_/)
We’re almost done... =(^.^)=
Last but not least, unless you’re creating a static website, your project uses services. Let’s define them:
Select all the services you are using: []
You have not selected any service, would you like to proceed anyway? [Yes]
┌───────────────────────────────────────────────────┐
│ CONGRATULATIONS! │
│ │
│ We have created the following files for your: │
│ - .environment │
│ - .upsun/config.yaml │
│ │
│ We’re jumping for joy! ⍢ │
└───────────────────────────────────────────────────┘
│ /
│/
│
(\ /)
( . .)
o (_(“)(“)
You can now deploy your application to Upsun!
To do so, commit your files and deploy your application using the Upsun CLI:
$ git add .
$ git commit -m 'Add Upsun configuration files'
$ upsun pushThe upsun project:init command (shortcut upsun ify) will automatically detect that you’re using an Express stack, ask if you want to add any services, and generate the corresponding config.yaml YAML files, like so:
applications:
app:
source:
root: "/"
type: "nodejs:18"
web:
commands:
start: "node index.js"
build:
flavor: none
dependencies:
nodejs:
sharp: "*"
#services:
# db:
# type: postgresql:14
routes:
"https://{default}/":
type: upstream
upstream: "app:http"
# A basic redirect definition
# More information: https://docs.upsun.com/define-routes.html#basic-redirect-definition
"https://www.{default}/":
type: redirect
to: "https://{default}/"Note
In the current guide, services are added to your project at a later stage.
Then commit your new files, using the following command:
git add .environment .upsun/config.yaml
git commit -m "Upsun config files" Set your project remote
Note
If you used the Upsun CLI command upsun project:create to create your project and your local Git repo has already been initialized, your local source code should already contain a .upsun/local/project.yaml file. This file contains your projectId, and you already have a Git remote repository set to upsun.
You can jump to deploying your project.
There are slightly different ways to link your local project to your Upsun project based on the Git workflow you chose for your project, as discussed earlier in this guide.
If you host your Express source code on an Upsun Git repository, and you failed to answer y (yes) to the question Set the new project <projectName> as the remote for this repository? [Y/n] during the project:create command, you need to let the Upsun CLI know which linked project you want to deploy to.
To do so, use the Upsun CLI to set a remote project:
upsun project:set-remote <projectId>This command will add a new remote called upsun to your local Git repo as you can see below:
git remote
upsun
...It will also create a new .upsun/local/project.yaml file that will contain the given <projectId>, to store this information for the Upsun CLI interaction.
Note
If you don’t remember your <projectId> from the previous steps, you can get it back using this command line and select the one you created:
upsun project:listWe assume here that you already have your own GitHub repository with the Express source code you want to host on Upsun. To link using this repository, use the following Upsun CLI command to create a GitHub integration with your project and then let the integration process create one environment per Git branch from your repository:
$ upsun integration:add
* Integration type (--type)
Enter a number to choose:
[0 ] bitbucket
[1 ] bitbucket_server
[2 ] github
[3 ] gitlab
[4 ] webhook
[5 ] health.email
[6 ] health.pagerduty
[7 ] health.slack
[8 ] health.webhook
[10] script
> 2
* Token (--token)
An authentication or access token for the integration
> X1234567890AZERTYUIOP
* Repository (--repository)
The repository (e.g. 'owner/repository')
> <owner>/<repository>We assume here that you already have your own Gitlab repository with the Express source code you want to host on Upsun. To link using this repository, use the following Upsun CLI command to create a Gitlab integration with your project and then let the integration process create one environment per Git branch from your repository:
$ upsun integration:add
* Integration type (--type)
Enter a number to choose:
[0 ] bitbucket
[1 ] bitbucket_server
[2 ] github
[3 ] gitlab
[4 ] webhook
[5 ] health.email
[6 ] health.pagerduty
[7 ] health.slack
[8 ] health.webhook
[10] script
> 3
* Token (--token)
An authentication or access token for the integration
> X1234567890AZERTYUIOP
* Repository (--repository)
The repository (e.g. 'owner/repository')
> <owner>/<repository>We assume here that you already have your own Bitbucket repository with the Express source code you want to host on Upsun. To link using this repository, use the following Upsun CLI command to create a Bitbucket integration with your project and then let the integration process create one environment per Git branch from your repository:
$ upsun integration:add
* Integration type (--type)
Enter a number to choose:
[0 ] bitbucket
[1 ] bitbucket_server
[2 ] github
[3 ] gitlab
[4 ] webhook
[5 ] health.email
[6 ] health.pagerduty
[7 ] health.slack
[8 ] health.webhook
[10] script
> 0
* Token (--token)
An authentication or access token for the integration
> X1234567890AZERTYUIOP
* Repository (--repository)
The repository (e.g. 'owner/repository')
> <owner>/<repository> Deploy
And just like that, it’s time to deploy!
Depending on the Git workflow you choose at the beginning of this tutorial, there are two ways to deploy your source code changes.
When using the Upsun Git repository as your main repository, you can push your code using the normal Git workflow (git add . && git commit -m "message" && git push upsun). This pushes your source code changes to your upsun remote repository. Alternatively, you can use the following Upsun CLI command:
upsun pushWhen using an external Git repository (GitHub, GitLab, or Bitbucket) to store your source code and having the Git integration feature enabled, on each code updates, you will need to use the normal Git workflow (git add . && git commit -m "message" && git push) to push your code to your external repository. To do so, run the following command:
git push originYour GitHub/GitLab/Bibucket integration process will then automatically create a new environment if you’re pushing a new Git branch, and deploy changes to your corresponding environment.
Upsun will now read your configuration files and deploy your project using default container resources. If you don’t want to use those default resources, define your own resource initialization strategy, or amend those default container resources after your project is deployed.
Et voilà, your Express application is live!
Tip
Each environment has its own domain name. To open the URL of your new environment, run the following command:
upsun environment:url --primary Make changes to your project
Now that your project is deployed, you can start making changes to it. For example, you might want to fix a bug or add a new feature.
In your project, the main branch always represents the production environment.
Other branches are for developing new features, fixing bugs, or updating the infrastructure.
To make changes to your project, follow these steps:
-
Create a new environment (a Git branch) to make changes without impacting production:
Terminalupsun branch feat-aThis command creates a new local
feat-aGit branch based on the main Git branch and activates a related environment on Upsun. The new environment inherits the data (service data and assets) of its parent environment (the production environment here). -
Make changes to your project.
For example, edit the
./index.jsfile and make the following changes:index.jsapp.get('/', async function(req, res){ // Make the output. const outputString = `Hello, World! - A simple Express web framework template for Upsun` res.send(outputString); }); -
Commit your changes:
Terminalgit add index.js git commit -m "Update Hello world" -
Deploy your changes to the
feat-aenvironment:Terminalupsun pushNote that each environment has its own domain name. To open the url of your new environment, run the following command:
Terminalupsun environment:url --primary -
Iterate by changing the code, committing, and deploying. When satisfied with your changes, merge them to the main branch, and remove the feature branch:
Terminalupsun merge Are you sure you want to merge feat-a into its parent, main? [Y/n] y upsun checkout main git pull upsun main upsun environment:delete feat-a git fetch --pruneNote
Deploying to production was fast because the image built for the
feat-aenvironment was reused.For a long running branch, to keep the code up-to-date with the main branch, use
git merge mainorgit rebase main. You can also keep the data in sync with the production environment by usingupsun env:sync.
Use a third-party Git provider
When you choose to use a third-party Git hosting service, the Upsun Git repository becomes a read-only mirror of the third-party repository. All your changes take place in the third-party repository.
Add an integration to your existing third-party repository: