-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathREADME.md
231 lines (134 loc) Β· 5.53 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# π listhen
<!-- automd:badges -->
[](https://npmjs.com/package/listhen)
[](https://npmjs.com/package/listhen)
<!-- /automd -->
Elegant HTTP listener!
[π Online Playground](https://stackblitz.com/github/unjs/listhen/tree/main/playground?startScript=dev)
## Features
β
Dev server with HMR, static, WebSockets and typescript support with <a href="https://github.com/unjs/jiti">unjs/jiti</a><br>
β
Works with Node.js, express, and <a href="https://github.com/unjs/h3">unjs/h3</a> out of the box <br>
β
Show the QR code of the public URL with <a href="https://github.com/unjs/uqr">unjs/uqr</a><br>
β
Tunnel your local server to the world with <a href="https://github.com/unjs/untun">unjs/untun</a><br>
β
Assign a port or fallback to a nicer alternative with <a href="https://github.com/unjs/get-port-please">unjs/get-port-please</a>
β
Gracefully shutdown Server with <a href="https://github.com/thedillonb/http-shutdown">http-shutdown</a><br>
β
Zero Config WebSockets with <a href="https://github.com/unjs/crossws">unjs/crossws</a>
β
Copy the URL to the clipboard<br>
β
HTTPS support with self-signed certificates<br>
β
Open URL in browser<br>
β
Detect test and production environments to auto-adjust behavior<br>
β
Close on the exit signal<br>
<div align="center">
<img width="100%" src="./.assets/screenshot.png">
</div>
## Quick Usage (CLI)
You can run your applications in localhost with typescript support and watch mode using `listhen` CLI:
Create `index.ts`:
```ts
export default (req, res) => {
res.end("Hello World!");
};
```
or using [unjs/h3](https://github.com/unjs/h3):
```ts
import { createApp, eventHandler } from "h3";
export const app = createApp();
app.use(
"/",
eventHandler(() => "Hello world!"),
);
```
or use npx to invoke `listhen` command:
```sh
npx listhen -w ./index.ts
```
## Usage (API)
Install package:
```bash
# pnpm
pnpm i listhen
# npm
npm i listhen
# yarn
yarn add listhen
```
Import into your Node.js project:
```js
// CommonJS
const { listen, listenAndWatch } = require("listhen");
// ESM
import { listen, listenAndWatch } from "listhen";
```
```ts
const handler = (req, res) => {
res.end("Hi!")
}
// listener: { url, getURL, server, close, ... }
const listener = await listen(handler, options?)
```
## Options
### `port`
- Default: `process.env.PORT` or 3000 or memorized random (see [get-port-please](https://github.com/unjs/get-port-please))
Port to listen.
### `hostname`
- Default: `process.env.HOST || '0.0.0.0'`
Default hostname to listen.
### `https`
- Type: Boolean | Object
- Default: `false`
Listen on HTTPS with SSL enabled.
#### Self-Signed Certificate
By setting `https: true`, listhen will use an auto-generated self-signed certificate.
You can set https to an object for custom options. Possible options:
- `domains`: (Array) Default is `['localhost', '127.0.0.1', '::1']`.
- `validityDays`: (Number) Default is `1`.
#### User-Provided Certificate
Set `https: { cert, key }` where the cert and key are paths to the SSL certificates.
With an encrypted private key, you also need to set `passphrase` on the `https` object.
To provide a certificate stored in a keystore set `https: { pfx }` with a path to the keystore.
When the keystore is password protected also set `passphrase`.
You can also provide an inline cert and key instead of reading from the filesystem. In this case, they should start with `--`.
### `showURL`
- Default: `true` (force disabled on a test environment)
Show a CLI message for the listening URL.
### `baseURL`
- Default: `/`
### `open`
- Default: `false` (force disabled on test and production environments)
Open the URL in the browser. Silently ignores errors.
### `clipboard`
- Default: `false` (force disabled on test and production environments)
Copy the URL to the clipboard. Silently ignores errors.
### `isTest`
- Default: `process.env.NODE_ENV === 'test'`
Detect if running in a test environment to disable some features.
### `autoClose`
- Default: `true`
Automatically close when an `exit` event, `SIGTERM`, `SIGINT` or `SIGHUP` signal is received in the process.
### `publicURL`
- Default: (the first public URL listening)
The public URL to show in the CLI output
### `qr`
- Default: `true`
Print QR Code for public address.
### `public`
- Default: `false` for development or when `hostname` is `localhost` and `true` for production
When enabled, listhen tries to listen to all network interfaces. You can also enable this option using `--host` CLI flag.
### `ws`
- Default: `false`
Enable experimental WebSocket support using [unjs/crossws](https://crossws.unjs.io/) or node upgrade handler.
Option can be a function for Node.js `upgrade` handler (`(req, head) => void`) or an Object to use [CrossWS Hooks](https://crossws.unjs.io/guide/api).
When using dev server CLI, you can easily use `--ws` and a named export called `websocket` to define [CrossWS Hooks](https://github.com/unjs/crossws) with HMR support!
## License
<!-- automd:contributors license=MIT author="pi0" -->
Published under the [MIT](https://github.com/unjs/listhen/blob/main/LICENSE) license.
Made by [@pi0](https://github.com/pi0) and [community](https://github.com/unjs/listhen/graphs/contributors) π
<br><br>
<a href="https://github.com/unjs/listhen/graphs/contributors">
<img src="https://contrib.rocks/image?repo=unjs/listhen" />
</a>
<!-- /automd -->
<!-- automd:with-automd -->
---
_π€ auto updated with [automd](https://automd.unjs.io)_
<!-- /automd -->