Make your Express.js app Cloud-Native with Appsody

Chris Bailey
Jul 15, 2019 · 4 min read

In the previous article: “Build a new Cloud Native Express.js app with Appsody”, you learned how to use Appsody to build an Express.js app that inherits a set of cloud-native capabilities from the Appsody Stack.

In this article, you’ll see how to take an existing Express.js app, and make it cloud-native by enabling it to work with the same nodejs-expressAppsody Stack.

Pre-requisites

In order to follow this tutorial, you’ll need to install the Apposdy CLI, which also requires you to have Docker installed, along with the Express.js application you want to make cloud-native with Appsody.

Install the Appsody CLI

Before getting started, you’ll need to install the Appsody CLI.

1. Follow the Installing Appsody guide to install the CLI for your platform.

Obtain an existing Express.js application

If you don’t have an existing Express.js application that you want to use, the following steps enable you to create one using the Express.js Application Generator. The examples in the article will assume you are using this.

1. Install the Express.js Application Generator:

npm install -g express-generator

2. Create a directory for your application:

mkdir expressjs-app
cd expressjs-app

3. Run the Express Application Generator to build a sample application:

express

Enabling your Express.js application with the `nodejs-express` Appsody Stack

New Appsody based applications are created using appsody init <stack>, where the name of the stack is one of those listed when running appsody list. This both downloads the most recent copy of the Appsody Stack, and populates the project directory with a template that provides a basic project structure.

In order to enable an existing application with a stack, the same steps can be followed, but using the --no-template option to appsody init as a project structure is not required.

1. Enable the Express.js Application:

appsody init nodejs-express --no-template

This will print output similar to the following to the console:

Running appsody init…
Downloading nodejs-express template project from https://github.com/appsody/stacks/releases/download/nodejs-express-v0.2.1/incubator.nodejs-express.templates.simple.tar.gz
Download complete. Extracting files from nodejs-express.tar.gz
Setting up the development environment
Running command: docker[pull appsody/nodejs-express:0.2]
Running command: docker[run — rm — entrypoint /bin/bash appsody/nodejs-express:0.2 -c find /project -type f -name .appsody-init.sh]
Successfully initialized Appsody project

As well as downloading the latest version of the Appsody Stack (in this case version 0.2), this has added a .appsody-config.yaml file to the project, which configures the versions of the Stack that the project will use.

While your application is now enabled to use the nodejs-express stack, there are three additional steps that are required to ensure that the Express.js application will work properly with the stack.

2. Modify the package.json file so that it has a main entry that references the file that contains your Express.js app object:

In the case of the application created by the Express Application Generator, the package.json becomes:

{
“name”: “expressjs-app”,
“version”: “0.0.0”,
“private”: true,
“main”: “app.js”,
“scripts”: {
“start”: “node ./bin/www”
},
“dependencies”: {
“cookie-parser”: “~1.4.4”,
“debug”: “~2.6.9”,
“express”: “~4.16.1”,
“http-errors”: “~1.6.3”,
“jade”: “~1.11.0”,
“morgan”: “~1.9.1”
}
}

Here main references app.js as that is the file that contains the Express.js app object. The next step is to export the app object from that file.

3. Export the Express.js app object from the file as module.exports.app:

The last line of the app.js file should become:

module.exports.app = app;

This exposes the app object so that the nodejs-express Appsody stack can apply it onto its own pre-configured Express.js server that includes the cloud-native capabilities.

4. Remove any default routes (eg. for 404: Not Found):

The nodejs-express stack contains default implementations of Health, Readiness and Liveness endpoints. These are designed to be able to be over-ridden by your application. These mean that any default route handles will over-ride those routes.

In the application created by the Express Application Generator, this means removing the following statement:

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

At this point, your application has been fully enabled. This means that you can now run, test and debug your application in a continuous containerized environment provided by the Appsody Stack, and you get the following additional capabilities, which have been inherited from the stack:

* Health Endpoint: http://localhost:3000/health
* Liveness Endpoint: http://localhost:3000/live
* Readiness Endpoint: http://localhost:3000/ready
* Metrics Endpoint: http://localhost:3000/metrics
* Performance Dashboard: http://localhost:3000/appmetrics-dash

You can also use build to build it into a optimized container image, and deploy to deploy it to Kubernetes.

Next Steps

To find out how to use the additional capabilities and commands, take a look at the previous articles on “Package your Node.js app for Cloud with Appsody” which covers using the run, test and debug commands, along with the Performance Dashboard, and “Build a new Cloud Native Express.js app with Appsody” which covers more on the nodejs-express Appsody Stack, and more on using the Metrics endpoint.

For more information on Appsody, join us on Slack, follow us on Twitter and star us on GitHub.

Appsody

Appsody (https://appsody.dev) provides tools and capabilities to create, develop, build and deploy cloud-native applications.

Chris Bailey

Written by

Appsody

Appsody

Appsody (https://appsody.dev) provides tools and capabilities to create, develop, build and deploy cloud-native applications.

More From Medium

More on Kubernetes from Appsody

More on Kubernetes from Appsody

Appsody in Autumn: project update

194

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade