{"id":18782,"library":"serve-once","title":"serve-once","description":"A utility for testing Express middleware by serving exactly one HTTP request then closing the server. It creates an HTTP server on a free port, runs the middleware, serves one request, and resolves with the response body. The current stable version is 3.0.2, released in early 2024. It supports GET, POST, PUT, DELETE, HEAD, and other methods. The package was rewritten in v3 to remove node-fetch dependency and requires Node >=18. It returns responses as string, JSON, buffer, or stream. It is lightweight and similar to supertest but simpler and focused on single-request testing.","status":"active","version":"3.0.2","language":"javascript","source_language":"en","source_url":"git://github.com/coderaiser/node-serve-once","tags":["javascript","express","middleware","server","serve","once","http","get","put"],"install":[{"cmd":"npm install serve-once","lang":"bash","label":"npm"},{"cmd":"yarn add serve-once","lang":"bash","label":"yarn"},{"cmd":"pnpm add serve-once","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used to extract the request body as a stream for methods like PUT/POST","package":"pullout","optional":false}],"imports":[{"note":"serve-once is a factory function that takes middleware and returns an object with request methods. It is not a named export in ESM. Use CommonJS require or wrap with createRequire.","wrong":"import { request } from 'serve-once'","symbol":"request","correct":"import { createRequire } from 'module'; const require = createRequire(import.meta.url); const { request } = require('serve-once')(middleware)"},{"note":"request is a function with methods like .get, .put, .post attached. Destructure or call as above.","wrong":"const request = require('serve-once')(middleware).request.get","symbol":"request.get","correct":"const { request } = require('serve-once')(middleware); request.get('/path')"},{"note":"The default export is a factory that takes middleware and optional default options. It returns an object with a request function.","wrong":"const request = require('serve-once')(middleware).request","symbol":"factory function","correct":"const serveOnce = require('serve-once'); const { request } = serveOnce(middleware, defaultOptions)"}],"quickstart":{"code":"import { createRequire } from 'module';\nconst require = createRequire(import.meta.url);\n\nconst pullout = require('pullout');\nconst serveOnce = require('serve-once');\n\nconst middleware = (options = 'hello') => (req, res) => {\n  res.end(JSON.stringify(options));\n};\n\nconst { request } = serveOnce(middleware);\n\nconst response = await request('get', '/');\nconsole.log(response); // 'hello'\n\nconst result2 = await request.get('/', {\n  options: 'world'\n});\nconsole.log(result2); // 'world'","lang":"javascript","description":"Demonstrates basic usage: create server with middleware, make GET request, get response as string. Also shows request.get() with custom options."},"warnings":[{"fix":"Upgrade Node to version 18 or later.","message":"v3 drops support for Node < 18. Requires Node >= 18.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure your environment supports global fetch (Node 18+).","message":"v3 removed node-fetch dependency; internal HTTP client uses native fetch (available in Node 18+).","severity":"deprecated","affected_versions":">=3.0.0"},{"fix":"Use const serveOnce = require('serve-once'); const {request} = serveOnce(middleware);","message":"The default export is not a class but a factory function. CommonJS require is typical as there is no named import.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"const result = await request('get', '/', { type: 'json' });","message":"The request function returns a response body as a string by default. To get JSON, set type option to 'json'.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure you call require('serve-once')(middleware) which returns an object with request method. Use const {request} = serveOnce(middleware); then request.get('/path')","cause":"Calling request.get before initializing the factory or using wrong import.","error":"TypeError: request.get is not a function"},{"fix":"Use CommonJS require via createRequire or use dynamic import: const serveOnce = (await import('serve-once')).default;","cause":"Importing serve-once as a named export in ESM.","error":"ERR_MODULE_NOT_FOUND: Cannot find module 'serve-once'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}