Skip to content

Node.js server

@discord0/adapter-node builds a Node.js HTTP server that you run yourself, on any machine or container that can serve HTTPS.

Terminal window
npx discord0 init my-bot --adapter node

discord0 deploy builds .discord0/node/index.mjs and syncs commands, but doesn’t upload anything. Start the server from that directory:

Terminal window
cd .discord0/node
DISCORD_PUBLIC_KEY=... DISCORD_BOT_TOKEN=... node index.mjs

It listens on PORT, or 3000, on every interface. Discord connects only over HTTPS, so put it behind a reverse proxy or load balancer that terminates TLS, and set https://<your domain>/interactions as the Interactions Endpoint URL.

To mount the app in an existing server, use node(app) as a request listener:

import { createServer } from "node:http";
import { node } from "@discord0/adapter-node";
import app from "./index";
createServer(node(app)).listen(3000);

In an Express-style server, mount it before any body parser, since the app verifies Discord’s signature against the raw body.

serve(app, { port, hostname, signals, tasks }) starts a server with graceful shutdown. The built index.mjs runs it.

The server is a long-lived process, so deferred work keeps running after the response with no host limit. Discord’s 15-minute token lifetime is the only bound.

On SIGINT or SIGTERM, the server stops accepting requests, waits for in-flight requests and deferred work to finish, then exits. A second signal exits at once. Pass signals: false to serve() to handle signals yourself, and call close() or the handler’s idle() to wait for pending work.

Deferred work lives in memory, so give your process manager a stop timeout longer than your slowest handler. Otherwise it kills the process before the work finishes:

serve() runs scheduled tasks in the server process, in each task’s time zone, while the server is up. Every copy of the server runs every task, so if you run more than one, set DISCORD0_TASKS=off in all but one, or pass tasks: false to serve(). On shutdown, the server waits for running tasks to finish.