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:
mkdir my-server && cd my-server
Set up a basic Node.js project:
npm init -y
Step 1.2: Create a basic server using Express
Install Express:
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 Node.js, copying the server code, and installing 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.
Install pkg:
npm i -D pkg
Create a binary:
npx pkg -t node18-alpine app.js
This creates a self-contained app binary that can run without installing Node.js inside the enclave. The binary takes the same name as the input JS filename.