Skip to main content

Step 1: Create a node.js server binary

Step 1.1: Set up a basic node.js project

Create and navigate to a fresh directory using

mkdir my-server && cd my-server

Set up a basic node.js project using

npm init -y

Step 1.2: Create a basic server using Express

Install express using

npm install express

Create a file named app.js with the following content

// Based on http://expressjs.com/en/starter/hello-world.html
const express = require('express')
const app = express()
const port = 4000

app.get('/', (req, res) => {
res.send('Hello World!\n')
})

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

Optional

You can test the server by running it locally using node app.js and navigating to http://localhost:4000 in your browser.

Step 1.3: Create a binary using pkg

Installing nodejs, copy-pasting the server code and then installing the npm packages as part of the image building process bloats the image size significantly. A smaller image size is achieved by packaging the server codebase into a single binary and creating an image around it.

Install pkg using

npm i -D pkg

Create a binary using

npx pkg -t node18-alpine app.js

This creates a self-contained app binary that can be run without installing nodejs inside the enclave. The binary takes the same name as the input js filename.