This is the Node.js code behind Integrate SurveyJS with Express, as an Express 5 application you can run example by example. Each example of that page has a page here that runs the real SurveyJS component against these routes, with a short "Try this" list, a live request log, a "What the server stored" panel and the code that just ran. SurveyJS has no backend of its own, so treat these as starting points for your own app.
- Live catalog: surveyjs-nodejs.demos.surveyjs.io
- SurveyJS demos: app.demos.surveyjs.io
- Backend integration examples on the SurveyJS website: surveyjs.io/backend-integration/examples
- The same examples for other platforms: PHP (Laravel), ASP.NET Core, Python (FastAPI), Next.js, Java (Spring Boot)
You need Node.js 24 or later, and nothing else: no Docker, no database server, no other service (see Prerequisites).
git clone https://github.com/surveyjs/surveyjs-nodejs-express.git
cd surveyjs-nodejs-express
npm ci # or npm install
npm run setup # creates .env for your private keys (from .env.example), storage/ and the SQLite database with the demo data
npm start # http://127.0.0.1:8000npm start also creates and seeds a missing database itself, so npm run setup only makes the first run explicit. While you work on the code, run npm run dev instead: Node's --watch restarts the app when a file changes.
- Fill Forms Together and Edit Forms Together use WebSocket relays that run inside the app, on its port (
/ws/rooms/<id>and/ws/forms/<id>). There is nothing else to start. - Run Server-Side Workflows (validate, lint, PDF, extract) run inside the app too, with the SurveyJS libraries (see Server-side workflows in process). Set
VALIDATE_RESPONSES=trueandLINT_DEFINITIONS=trueinsettings.envto switch Validate Responses and Lint JSON Schemas on, and an AI provider for Extract Responses from Documents (its key goes in.env). - The tests:
npm test(node --test, a few seconds, no browser and no network),npm run contract(the shared contract test against a fresh database;npm run contract -- --smokefor the smoke test) andnpm run test:browser(the shared Playwright specs; runnpx playwright install chromiumonce).
npm start checks that nothing answers on the port first, then prints the folder it serves and its <branch>@<commit>:
Serving C:\src\surveyjs-nodejs\ (master@4a96b83) on http://127.0.0.1:8000
- Node.js 24 or later (26 LTS recommended).
npm cion Node 22 or older stops with npm's ownEBADENGINEerror naming the version (enginesinpackage.json,engine-strictin.npmrc), andnode server.jssays which version to install. sharp, which the extractor uses to find a document's QR code, installs a prebuilt binary: no build tools are needed..npmrcsetsignore-scripts=true, so no package runs an install script.
On Windows:
-
Install Node.js 26 LTS from nodejs.org or with
winget install OpenJS.NodeJS.LTS. -
After installing, restart your editor, not only its terminal: VS Code's terminals inherit VS Code's environment. To refresh the current PowerShell terminal instead:
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') + ';' + [Environment]::GetEnvironmentVariable('Path','User') node --version
-
npm runs scripts in
cmd, soVAR=value npm startdoesn't work there. Set a variable for one run like this, or put it insettings.env:$env:APP_PORT=8001; npm start
APP_PORT=8001 npm start
On Windows, a second server can bind a port another server already listens on (one on 0.0.0.0, one on 127.0.0.1) while the first keeps answering. So npm start and npm run dev connect to the port first and stop with "Port 8000 is already in use. Another copy of this app (another checkout or branch?) may be running. Stop it, or set APP_PORT." The contract test's AI stub uses a free port of its own (AI_STUB_PORT, default 3020, when you start it by hand).
Ctrl+C in the terminal: the app closes the relays, the connections and the database, and the port is free at once. To find a server left running in another terminal or checkout:
Get-NetTCPConnection -LocalPort 8000 -State Listen | Select-Object OwningProcess
Stop-Process -Id <OwningProcess>lsof -i :8000
kill <PID>With Docker, docker compose down.
- Check the branch out in its own folder, with
git worktree add ../surveyjs-nodejs-pr <branch>(for a pull request,git fetch origin pull/<number>/head:pr-<number>first) or a second clone, and runnpm cithere.node_modules/,.envandstorage/belong to each checkout. - Run one checkout on the default port at a time: stop the other one first, or start the second with
APP_PORT=8001(PowerShell:$env:APP_PORT=8001; npm start). - The page says which checkout answered: "Running
<branch>@<commit>" under the catalog's footer buttons and at the end of every example's Description panel.
docker compose up --build # http://127.0.0.1:8000The image (Dockerfile) is node:26-slim with npm ci --omit=dev, runs as the image's node user with NODE_ENV=production, and starts with node server.js (not npm start, which doesn't pass SIGTERM on). It creates and seeds the database on its first start. Its one volume, /app/storage, holds the database, the uploads, the visitor sandboxes and the relay snapshots. Logs go to stdout and stderr (docker compose logs). One process handles every visitor: extraction and translation wait on the network without blocking anyone, and linting, validation and PDFs take a few milliseconds each (measured: 15–55 ms for a claim PDF, under 6 ms to lint the largest seeded form).
The app trusts X-Forwarded-For, X-Forwarded-Proto and X-Forwarded-Host from any proxy (app.set("trust proxy", true) in app.js), so behind a TLS proxy its absolute URLs and the relays' wss:// URLs use the scheme the visitor sees, and Secure cookies work. In your own production, narrow it to the proxy's address (app.set("trust proxy", "loopback"), or a subnet) and don't publish the app's port. The relays are on the app's own host and port, so the proxy needs no extra route, only WebSocket upgrades passed through. For Traefik, next to your entry point and TLS labels:
services:
app:
labels:
- traefik.http.routers.surveyjs.rule=Host(`example.com`)
- traefik.http.services.surveyjs.loadbalancer.server.port=8000Traefik passes WebSocket upgrades on by default. RELAY_URL and EDIT_RELAY_URL are only needed when /ws/ is served from another origin.
Two files hold the settings. settings.env is committed and holds every setting except the private keys. .env is git-ignored and holds only the private keys: SURVEYJS_LICENSE_KEY, AI_API_KEY, OPENAI_API_KEY and ANTHROPIC_API_KEY (npm run setup copies .env.example to it). npm start loads both with Node's own --env-file (no dotenv): variables set in the environment win, then .env, then settings.env. The Docker image loads settings.env the same way, and docker compose passes .env in. config.js reads every setting once, and no other file reads process.env. Switches accept 1, true, on or yes.
| Variable | Default | What it does |
|---|---|---|
APP_PORT |
8000 |
Where the app listens. Wins over PORT. |
PORT |
unset | Set by Azure App Service and Heroku; used when APP_PORT isn't. |
APP_HOST |
127.0.0.1 (0.0.0.0 when PORT is set) |
The address the app binds. |
NODE_ENV |
development |
production caches templates and hides stack traces. |
DATA_DIR |
storage |
The database, uploads, sandboxes and relay snapshots. |
SURVEYJS_LICENSE_KEY |
empty | Calls setLicenseKey() on every page and, at startup, on the server, so it covers server PDFs too. Without it, the commercial components show a licence banner. |
VALIDATE_RESPONSES |
false |
Validate Responses: routes/validate-response.js replaces routes/save-response.js. |
LINT_DEFINITIONS |
false |
Lint JSON Schemas: routes/lint-definition.js replaces the PUT in routes/creator-load-save.js. |
SURVEYJS_AI_PROVIDER, SURVEYJS_AI_MODEL, SURVEYJS_OLLAMA_BASE_URL, OPENAI_API_KEY, ANTHROPIC_API_KEY |
empty | Extract Responses from Documents: the extractor's provider (openai, anthropic or ollama), chosen as SurveyJS Server chooses it. Without SURVEYJS_AI_PROVIDER, the provider whose key is set. |
AI_BASE_URL, AI_API_KEY, AI_MODEL |
OpenAI, empty, gpt-4o-mini |
Translate with AI: any OpenAI-compatible /chat/completions endpoint. Without a key, /api/translate answers 501. |
RELAY_URL, EDIT_RELAY_URL |
empty | The relays' origin when a proxy serves /ws/ elsewhere, for example wss://example.com. Empty: the page's own origin. |
DEMO_MODE |
false |
The hosted demo's visitor sandboxes (below). |
SURVEYJS_BUILD |
from git | The <branch>@<commit> the pages show as "Running …". Hosted demos set it at deploy. |
SURVEYJS_DOCS, SURVEYJS_GITHUB |
the Node.js docs page, this repository | Where the pages link. |
There is no SURVEYJS_SERVICE_URL: Run Server-Side Workflows don't call a service here. The in-process code is in routes/validate-response.js, lint-definition.js, server-pdf.js and extract-from-paper.js, with integration/surveyjs.js and integration/ai-provider.js.
Each example is two or three files you can read on their own. The full explanation is in the docs.
routes/<slug>.js: the example's routes, the server code the docs show, asexport default function (app) { app.post(…) }.app.jsmounts them in the docs' order, and picksvalidate-response.jsorsave-response.js, andlint-definition.jsorcreator-load-save.js, by the switches.shared/client/<slug>.js: the example's client code, a plain ES module, the same file on every platform.views/examples/<slug>.njk: the example's page: its description, the "Try this" list and the component.relay/fill-relay.jsandrelay/edit-relay.js: the relays of Fill Forms Together and Edit Forms Together, with their rooms inrelay/fill-room.jsandrelay/edit-room.js.relay/index.jsattaches them to the app's HTTP server.integration/: small helpers some routes call, so the code the docs show stays about SurveyJS. They aren't demo code:stored-json.js(save-response,relational-storage,validate-response, the fill relay): inserts a response as one JSON document, with an ISO 8601 UTCcreated_at.surveyjs.jsandai-provider.js(Run Server-Side Workflows): the linter options, the response errors, the PDF call, the extraction timeout and provider, and the logging.ai-translation.js(Translate with AI): the call to your OpenAI-compatible provider.http.js,cache.js,file-type.js: errors with a status, fetch with a timeout, the countries cache, and file types by their first bytes.
db.js(db(req), SQLite throughnode:sqlite) andauth.js(can(),canReadFile()).
To copy an example, copy its route file and the helpers it imports. The code the docs show is cut from these files between // #region sjs:<slug>.server (or .client) and // #endregion. surveyjs-integration.json lists every example's files and regions; npm run check verifies them, and npm run contract runs the shared contract test (shared/contract-tests/run.mjs) against a fresh database. Any of better-sqlite3, Prisma or Knex works just as well as node:sqlite; the examples use plain SQL so that a response stays visibly one JSON document.
shared/ holds what every platform repository uses byte for byte: the client modules, the form definitions, samples and seed data, the page UI (shared/ui/), and the contract, smoke and browser tests. It is a copy of surveyjs-php-laravel's shared/ at the commit in shared/UPSTREAM; npm run check compares it with shared/SHA256SUMS. Don't edit it here: send a fix to that repository, then copy shared/ again.
The text in an example's Description panel is the description block of views/examples/<slug>.njk. It can hold several paragraphs, links and other markup:
{% block description %}
<p>Your custom description.</p>
<p>More details with <strong>formatting</strong>.</p>
{% endblock %}The "Try this" list is the try block; the layout is views/layouts/example.njk. The catalog cards use need from surveyjs-integration.json for their "Use when" text.
A response, a definition, saved progress and variable presets are each one JSON document, stored as text and returned as stored.
express.json({ limit: "12mb", strict: false })inapp.jsparses every body. The limit is above the 100 KB default because a Creator definition with images is bigger.strict: falsehands any JSON value to the handler, so a top-levelnullor string gets the handler's own400, not a parse error. A body that isn't JSON leavesreq.bodyundefined, which every handler treats as a wrong envelope.- The handlers store
JSON.stringify(req.body…)and nothing else. They never copy the parsed document (spread,Object.assign,structuredClone):JSON.parsecreates a"__proto__"key as an ordinary property, and a copy would turn it into the copy's prototype. JSON.parsekeeps{},[{}],null,"", whitespace inside strings and non-ASCII text.1.0becomes1, because JavaScript has one number type; that only matters if you compare stored JSON as text.created_atis ISO 8601 UTC without milliseconds (2026-11-15T12:00:00Z), so the dashboard's date filters are string comparisons;integration/stored-json.jsformats it.
SurveyJS Server (surveyjs/surveyjs-server) is itself an Express 5 app. Here the same libraries run in the app, following its source at db1ac24, so these routes accept and reject what the service does:
routes/validate-response.js:lintSurvey()fromsurvey-core/linteron the stored definition, thennew Model(definition),setData(data, {})andvalidate()fromsurvey-core.routes/lint-definition.js:lintSurvey()with the service's rule overrides.routes/server-pdf.jsand the blank work order inroutes/extract-from-paper.js:new SurveyPDF(definition)fromsurvey-pdf(inintegration/surveyjs.js).routes/extract-from-paper.js:createExtractor()andextractFromImage()fromai-form-response-extractor, with the provider fromintegration/ai-provider.js.
A failure in the libraries is this app's own error (500); 502 and 504 are kept for the AI provider. SURVEYJS_LICENSE_KEY covers server PDFs here, because server.js calls setLicenseKey() at startup. When your backend isn't Node.js, run the same code as a separate service: SurveyJS Server, which the PHP and ASP.NET Core examples call over HTTP.
Locally, everyone shares one SQLite database, storage/database.sqlite (delete storage/ to start over).
On the hosted demo (DEMO_MODE=true), each visitor gets a private copy of the seeded database and uploads folder, chosen by a demo_sid cookie, and kept until it has been unused for 24 hours (an hourly timer in server.js). The "Open a second window" link in Fill Forms Together and Edit Forms Together carries that id (?join=), so whoever opens it works in the same copy and the same relay rooms. The demo also caps uploads at 5 MB and the AI examples at 20 calls per visitor per day, counted in this process: with several processes, keep the counts in a shared store. None of this is in the example code.
- The pages are Nunjucks templates (
views/), autoescaped. Nunjucks has layouts and blocks ({% extends %},{% block %}) that map one to one onto the Blade views of the reference repository, so each page diffs against its PHP source in template syntax only, with the Tailwind class strings unchanged; EJS, the most common Express engine, has no layouts or blocks, and Pug would rewrite every element. Its syntax is also the Jinja syntax of the Flask port. (LiquidJS has the same layout and block model, if you prefer a maintained alternative.) Inline JSON goes through onescript_jsonfilter (app.js), which escapes<,>,&, U+2028 and U+2029, so a</script>in a definition can't end the script. - The site stylesheet is Tailwind CSS v4 without Preflight:
styles/site.cssimportstailwindcss/theme.css(static),tailwindcss/utilities.cssandshared/ui/theme.css.npm run cssbuilds it intopublic/css/site.csswith@tailwindcss/cli. The output is committed, sonpm startnever builds CSS; CI rebuilds it and fails on a difference. - The client code is the shared ES modules in
shared/client/andshared/ui/, loaded unchanged with<script type="module">and an import map (demo/import-map.js). The SurveyJS packages come from this app's ownnode_modules(/vendor/<package>@<version>/, pinned toshared/surveyjs-version.json), and the others (chart.js,lodash-es, highlight.js) from unpkg at pinned versions. There is no bundler. - The forms use the SurveyJS Tailwind adapter (
survey-core/themes/adapters/tailwind4.css). No page callsapplyTheme(): its inline styles would override the adapter.
The routes only use the request, the database and the response, so they port in a few lines. Save Responses in Fastify 5, whose JSON parser also keeps {}:
fastify.post("/api/responses", async (request, reply) => {
const { formId, data } = request.body ?? {};
if (formId == null || data == null) return reply.code(400).send({ error: "Expected { formId, data }" });
const { lastInsertRowid } = db.prepare("INSERT INTO responses (form_id, data, created_at) VALUES (?, ?, ?)")
.run(formId, JSON.stringify(data), new Date().toISOString().replace(/\.\d{3}Z$/, "Z"));
return reply.code(201).send({ id: Number(lastInsertRowid) });
});The same goes for the middleware this demo leaves out: helmet, cors, morgan, compression, express-rate-limit, cookie-parser or express-session are each a reasonable choice in a real app. They aren't here so that every line you read is about SurveyJS.
These examples must not be used as a real service as they are. They don't cover authentication, authorization, user management, access levels and other security aspects of a real survey service. The places where real checks go are marked:
can(user, "edit-forms")andcanReadFile(user, file)inauth.js: who may save definitions and presets, and who may read an uploaded file.demo/users.js: the demo-user switch stands in for your session or token middleware (express-session, Passport, a JWT check). The relays check the same cookie inrelay/index.js.
When you copy an example into your app, leave out the demo-only code: the demo/ folder (and its lines in app.js, server.js and relay/index.js), pages.js, views/, shared/ui/ (the page frame, its tabs and highlighting, the fonts and site images) and shared/client/host.js (its mountSurvey() is one line: renderSurvey(survey, element) from survey-js-ui), and relay/room-key.js's sandbox keys.
The code in this repository is available under the MIT licence.
The examples use SurveyJS Form Library, which is MIT-licensed as well. The examples marked "Commercial" in the Licence column above also use SurveyJS Survey Creator, Dashboard or PDF Generator, which are commercial products. You can integrate them and evaluate their functionality without a licence. Production use requires a developer licence for each developer who works with the SurveyJS APIs or implements the integration. See the SurveyJS Licensing FAQ and the End-User License Agreement.