Skip to content

Commit

Permalink
feat(node-server): support listening to unix sockets using `NITRO_UNI…
Browse files Browse the repository at this point in the history
…X_SOCKET` (#1201)

Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
uroybd and pi0 authored Aug 20, 2023
1 parent 4a6cbcb commit 6a1ea5c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/content/2.deploy/1.node.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ You can customize server behavior using following environment variables:

- `NITRO_PORT` or `PORT` (defaults to `3000`)
- `NITRO_HOST` or `HOST`
- `NITRO_SOCKET` - if provided (a path to the desired socket file) the service will be served over the provided UNIX socket.
- `NITRO_SSL_CERT` and `NITRO_SSL_KEY` - if both are present, this will launch the server in HTTPS mode. In the vast majority of cases, this should not be used other than for testing, and the Nitro server should be run behind a reverse proxy like nginx or Cloudflare which terminates SSL.
- `NITRO_SHUTDOWN` - Enables the graceful shutdown feature when set to `'true'`. If it's set to `'false'`, the graceful shutdown is bypassed to speed up the development process. Defaults to `'false'`.
- `NITRO_SHUTDOWN_SIGNALS` - Allows you to specify which signals should be handled. Each signal should be separated with a space. Defaults to `'SIGINT SIGTERM'`.
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/entries/node-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,28 @@ const port = (destr(process.env.NITRO_PORT || process.env.PORT) ||
3000) as number;
const host = process.env.NITRO_HOST || process.env.HOST;

const path = process.env.NITRO_UNIX_SOCKET;

// @ts-ignore
const listener = server.listen(port, host, (err) => {
const listener = server.listen(path ? { path } : { port, host }, (err) => {
if (err) {
console.error(err);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
const protocol = cert && key ? "https" : "http";
const addressInfo = listener.address() as AddressInfo;
if (typeof addressInfo === "string") {
console.log(`Listening on unix socket ${addressInfo}`);
return;
}
const baseURL = (useRuntimeConfig().app.baseURL || "").replace(/\/$/, "");
const url = `${protocol}://${
addressInfo.family === "IPv6"
? `[${addressInfo.address}]`
: addressInfo.address
}:${addressInfo.port}${baseURL}`;
console.log(`Listening ${url}`);
console.log(`Listening on ${url}`);
});

// Trap unhandled errors
Expand Down

0 comments on commit 6a1ea5c

Please sign in to comment.