How to deploy an Express Application with ServerSinc
Tags: devops, node, tutorial, automation
Published: 2023-09-15 13:52:45

## What is Express?

Express.js, often referred to as Express, is a lightweight framework for building web and mobile applications in node.js. It's simplicity and flexibility allows developers to design and build scalable and efficient server-side applications for a wide range of purposes. 


## Your first Express app

For this guide, we'll be using the basic express app available on our Github. 


```
const express = require("express");
const cors = require("cors");
const dotenv = require("dotenv");

dotenv.config();

const port = process.env.PORT
const app = new express();

app.use(cors());

app.get("/v1/message", (request, response) => {
  const { message } = request.query;

  return response.json({
    message,
  });
});

app.get("/v1/users", (_, response) => {
  return response.json({
    users: ["Homer", "Marge", "Bart", "Lisa", "Maggie"],
  });
});

app.listen(port, () => {
console.log(`Express app listening on port ${port}`);
});
```

For the Express app to work, we'll also need a .env file with PORT variable set to 3000.

## Pushing to GitHub

As ServerSinc relies on Github to deploy your code, you'll need to push the latest version of the express app to your own repo. 

Initialize Git: Navigate to your project folder in the command line and run:

`git init`

Create a GitHub Repository: On GitHub, create a new repository and note the repository URL.

If you have the Github CLI tool installed, running gh repo create [name] --private  will create a new private repo for you.

Add a Remote: Use this command, replacing <repository_url> with your GitHub repository URL:

`git remote add origin <repository_url>`

Push Your Code: Push your code to GitHub's "master" branch (or your branch) with:

`git push -u origin master`

## Setting up our Express App

Log into your ServerSinc account and click Servers, then view the Server you want to deploy the app to. If you don't have one already, create or import a new one.

Next, go to Applications and click 'Create Application'.

![](https://a.storyblok.com/f/249646/816x668/fb52b5edfd/create-app-form.png)

Start by providing a Name for your app in the designated field. Optionally, you can also specify a Domain for your app.

Under Repository, choose your Express app's repository. A new dropdown menu will appear, prompting you to select the specific Branch you wish to use. 

Additionally, you have the option to enable "Automatic Deploys," which will automatically deploy your app with each new commit made to the selected branch.

As our application is written in JavaScript, we don't need to add anything to the Build Directory input.

In the "Index File" field, input the entry point file of your Express app. In most cases, this will be "index.js.".

Finally, enter the PORT value that you previously defined in your `.env` file and click 'Create Application'

This will not deploy your app straight away.

## Configuring Deploy Script and Environment Variables

Once your app has been created, you'll see the application overview page.

![](https://a.storyblok.com/f/249646/1580x603/4b89298b2d/app-overview.png)

Firstly, we'll delete the "<!-- Your Code Here -->" line from the deploy script and then save it. 
The deploy script is run on your server each time your code is ready to deploy, here we can run build scripts, migrations and more.

Next, we'll navigate to the "Environment Variables" tab and add our PORT variable.  

### Domain Settings

If you added a domain to your application during creation, you'll see the domain in the "Domain & SSL" tab. During the first deploy, ServerSinc will automatically configure NGINX to point the domain at your application.

## Triggering a Deploy

Finally, click 'Deploy' and navigate to the "Activity" tab to see the latest deploy. Once the app has deployed, you'll see a green tick against the deploy.

To view the deploy logs, click the logs icon at the end of each completed deploy, this is handy way of knowing if everything went smoothly during deployment.

![](https://a.storyblok.com/f/249646/1603x530/b2f085fcca/logs-view.png)

Voila!

Navigate to your servers IP , or Domain, to see your application live!