Skip to main content

Node Express on Section

Learn how to run a "Hello World" Node Express app at the edge for low latency and high availability. You can use our repo as a template, or perform the steps yourself using the Kubernetes dashboard or kubectl commands.

What You'll Build

Short on time? Visit https://floral-smoke-4415.section.app to see what you'll be building.

note

Before starting, create a new Section Project and then delete the default Deployment and ingress-upstream Service to prepare the project for your new deployment.

Option 1 - Copy Our GitHub Repo

  1. Make a new repo from our template: in your browser visit https://github.com/section/node-express and select Use this template (don't clone, don't fork, but use the template). Choose yourself as an owner, give it a name of your choice, and make it be Public (not Private).
  2. In your new GitHub repo, under Settings > Secrets > Actions, use New repository secret to add these two:
  3. Make a simple change to the HTML text in views\pages\index.ejs and watch your changes go live.

Every time you push to the repo your project will be built and deployed to Section automatically using GitHub Actions.

Option 2 - Step by Step

Following are step-by-step instructions to deploy a Node.js Express "Hello World" application to the edge on Section. We'll Dockerize it, push it to GitHub Packages, and deploy it on Section.

Prerequisites

  • You need Docker and Node installed so that you can build a docker image.

Create the Node.js App

Create a new directory for your app.

mkdir node-express
cd node-express

Initialize the package.json file, which handles metadata for Node.js. Answer yes in response.

npm init
...
Is this OK? (yes)

Now install Express with npm install express --save.

And then we add our application code. Create app.js with the following code.

app.js
const express = require('express')
const app = express()
const port = process.env.PORT || 3000

app.get('/', (req, res) => {
res.send( "<h1>Hello World from Node Express on Section!</h1>" );
})

app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`)
})

Test it using node app.js and visit it using curl http://localhost:3000. You'll get the "Hello World from Node Express on Section!" response.

Dockerize It

Let's build the container image that we'll deploy to Section. First make a Dockerfile in your directory with the following content.

Dockerfile
FROM node:lts as runner
WORKDIR /node-express
ENV NODE_ENV production
ARG COMMIT_ID
ENV COMMIT_ID=${COMMIT_ID}
COPY . .
RUN npm ci --only=production
EXPOSE 3000
CMD ["node", "app.js"]

Build and tag it.

docker build . -t ghcr.io/YOUR_GITHUB_USERNAME/node-express:prod

Launch it locally to test it.

docker run -p 3000:3000 ghcr.io/YOUR_GITHUB_USERNAME/node-express:prod
curl http://localhost:3000

Push It

Push it to GitHub Packages. This makes it available to Section.

docker push ghcr.io/YOUR_GITHUB_USERNAME/node-express:prod

Be sure to make it public. To see your packages and make this change, visit https://github.com/YOUR_GITHUB_USERNAME?tab=packages

Deploy It

Next, create a Section deployment for app with a file node-express-deployment.yaml, substituting YOUR_GITHUB_USERNAME accordingly. This will direct Section to distribute the container you've pushed to GitHub Packages.

node-express-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: node-express
labels:
app: node-express
spec:
replicas: 1
selector:
matchLabels:
app: node-express
template:
metadata:
labels:
app: node-express
spec:
containers:
- name: node-express
image: ghcr.io/YOUR_GITHUB_USERNAME/node-express:prod
imagePullPolicy: Always
resources:
requests:
memory: ".5Gi"
cpu: "500m"
limits:
memory: ".5Gi"
cpu: "500m"
ports:
- containerPort: 80

Apply this deployment resource to your Project with either the Kubernetes dashboard or kubectl apply -f node-express-deployment.yaml.

Expose It

Expose it on the internet, mapping the container's port 3000.

ingress-upstream.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: ingress-upstream
name: ingress-upstream
spec:
ports:
- name: 80-8080
port: 80
protocol: TCP
targetPort: 3000
selector:
app: node-express
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}

Apply this service resource to your Project with either the Kubernetes dashboard or kubectl apply -f ingress-upstream.yaml.

See the pods running on Section's network with either the Kubernetes dashboard or kubectl get pods -o wide. The -o wide switch shows where your app is running according to the default AEE location optimization strategy. Your app will be optimally deployed according to traffic. In lieu of significant traffic, your deployment will be made to default locations.

Node Express pods

Try kubectl logs POD to see the log message reporting that the server is listening on port 3000 (App listening at http://localhost:3000)

Finally, follow the instructions that configure DNS and TLS.

See What You've Built

See the "Hello World" app you've built by visiting the https://YOUR.DOMAIN.COM, substituting YOUR.DOMAIN.COM according to your DNS and HTTPS configuration.