https://github.com/fvilers/listen-once
A Node.js web server that listen only once
https://github.com/fvilers/listen-once
server typescript typescript-library web
Last synced: 2 months ago
JSON representation
A Node.js web server that listen only once
- Host: GitHub
- URL: https://github.com/fvilers/listen-once
- Owner: fvilers
- License: mit
- Created: 2023-07-13T08:05:09.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-13T09:17:51.000Z (almost 3 years ago)
- Last Synced: 2025-02-26T13:49:11.105Z (over 1 year ago)
- Topics: server, typescript, typescript-library, web
- Language: TypeScript
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# listen-once
A Node.js web server that listen only once
## Purpose
This library expose a simple method that spins a web server that accept only one request. It can be used to retrieve a query string parameter or body from a authorization redirect (OAuth 2 for example).
# ECMAScript module
This library is published as an ECMAScript module.
## Usage
```typescript
import http from "node:http";
import { listenOnce } from "@fvilers/listen-once";
const PORT = 3000;
const HOSTNAME = "localhost";
function onListening() {
console.log("Server listening on", `http://${HOSTNAME}:${PORT}`);
}
function onRequest(req: http.IncomingMessage, res: http.ServerResponse) {
console.log("Incoming request", req.url);
// TODO: do something with the request
res.statusCode = 200;
res.end();
}
await listenOnce(PORT, HOSTNAME, onListening, onRequest);
console.log("Server closed");
```