Skip to content

Latest commit

 

History

History
75 lines (51 loc) · 1.9 KB

README.md

File metadata and controls

75 lines (51 loc) · 1.9 KB

Nodesockd

This tool is the missing link between a Nginx reverse proxy and a Node.js backend service. Typically, setups like this bind the backend service to a port on the loopback interface, which is secure enough, but adds some overhead. There are some pitfalls to switching over to UNIX sockets though, and this package aims to address them in one fell swoop:

  • atomic deploy, even with database migrations
  • multiple workers supported
  • standby workers for fast failover

Installation

npm install --save nodesockd

Quick start

Create a file called nodesockd.yaml in your application's root directory with the following contents:

# nodesockd.yaml
script: build/app.js  # the entrypoint of your app
tmpDir: tmp           # make sure this directory exists!

Next, in your app entrypoint, you'll need to do something like this:

// src/app.ts

import { nodesockd } from 'nodesockd';
import * as express from 'express';

const app = express();

// register routes etc.

const server = app.listen(nodesockd.socketPath ?? 1234, async () => {
  // tell Nodesockd that the worker is ready to start serving requests:
  await nodesockd.reportOnline();
});

nodesockd.on('shutdown', () => {
  // when Nodesockd tells the worker to shut down:
  server.close();
});

Now you can run your app like this:

# through Nodesockd - will listen on port 8000:
node_modules/.bin/nodesockd daemon --dev-server

# change the dev server listen port:
node_modules/.bin/nodesockd daemon --dev-server=8123

# without Nodesockd - will listen on the fallback port 1234
# defined in the app code:
node build/app.js

Of course, this is what you might use for local development; in production, you'll need to set some things up to correctly expose the application sockets to Nginx. Refer to the documentation for more details.