SpaceNode
A minimal, production-ready microservice with health checks, security, compression, and graceful shutdown.
import { createApp, defineAuth } from 'SpaceNode'
// Auth (e.g. internal API key)
defineAuth(async (token) => {
if (token === process.env.API_KEY) return { role: 'service' }
return null
})
const app = await createApp({
pipe: ['security', 'compress', 'logger', 'rateLimit:200'],
bodyLimit: 512 * 1024, // 512 KB
timeout: 10000,
openapi: {
title: 'Payment Service',
version: '1.0.0',
},
})
// Health & readiness endpoints
app
.setRoute('GET', '/health', ({ send }) => {
send({
status: 'ok',
uptime: Math.floor(process.uptime()),
memory: process.memoryUsage().rss,
})
})
.setRoute('GET', '/ready', ({ send }) => send({ ready: true }))
// Error monitoring
app.onError((err, req) => {
console.error(`[${req.method}] ${req.path}:`, err.message)
// Send to Sentry, Datadog, etc.
})
// Start
const PORT = process.env.PORT || 3000
app.listen(PORT)
// Graceful shutdown
const shutdown = () => {
console.log('Shutting down...')
app.close(() => process.exit(0))
}
process.on('SIGINT', shutdown)
process.on('SIGTERM', shutdown)
FROM node:20-alpine
WORKDIR /app
COPY package.json .
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
security pipe (HSTS, X-Frame, etc.)compress pipe/health for load balancersonError()/openapi.json