0.1.2 • Published 5 months ago

node-temp-server v0.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

npm GitHub

node-temp-server

A temporary HTTP server that waits for a single request and then shuts down. Useful for testing HTTP clients or implementing mock servers for tests, or waiting for redirects for OAuth flows.

Installation

npm install node-temp-server

Usage

import { waitForRequest } from "node-temp-server";

// Basic usage
const requestData = await waitForRequest();
// Server will listen on localhost:3000 and shut down after first request

// Custom configuration
const requestData = await waitForRequest({
  host: "localhost",
  port: 3000,
  path: "/api",
  timeout: 5000,
  responseStatus: 200,
  responseHeaders: { "Content-Type": "application/json" },
  responseBody: JSON.stringify({ message: "Success" }),
});

// The returned requestData contains:
// - query: Record<string, string> - parsed query parameters
// - body: any - parsed JSON body (if present)
// - rawRequest: http.IncomingMessage - raw Node.js request object

Options

OptionTypeDefaultDescription
hoststring'localhost'Server host
portnumber3000Server port
pathstring'/'Path to match requests against
timeoutnumber60000Timeout in milliseconds
responseStatusnumber200Response status code
responseHeadersobject{'Content-Type': 'text/plain'}Response headers
responseBodystring'OK'Response body

Example

// Wait for POST request with custom response
const { body, query } = await waitForRequest({
  path: "/webhook",
  port: 3000,
  responseStatus: 201,
  responseHeaders: {
    "Content-Type": "application/json",
  },
  responseBody: JSON.stringify({ received: true }),
});

console.log("Request body:", body);
console.log("Query parameters:", query);
0.1.2

5 months ago

0.1.1

5 months ago