0.1.0 • Published 3 months ago

@exotjs/express v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

Express Middleware for Exot Inspector

This package includes an Express middleware for automatic request tracing, an error handler for tracking Express errors, and a WebSocket server for communication between the server and the app.

Features

  • Automatic request tracing (use middleware to enable)
  • Error tracking (use errorHandler to enable)
  • Built-in WebSocket server for server-app communication channel

Usage

import { createServer } from 'node:http';
import { errorHandler, middleware, websocketServer } from '@exotjs/express';
import { Inspector } from '@exotjs/express/inspector';
import { MemoryStore } from '@exotjs/express/store';
import express from 'express';

const app = express();

// 1. Instantiate the inspector
const inspector = new Inspector({
  store: new MemoryStore(),
});

const server = createServer(app);

// 2. Mount the middleware
app.use(middleware({
  inspector,
}));

app.get('/', (req, res) => {
  res.send('Hello');
});

// 3. Mount the error handler
app.use(errorHandler({
  inspector,
}));

server.listen(3002);

// 4. Start the websocket server
websocketServer({
  authorize: async (req) => {
    // check request somehow, throw an error if not authorized
  },
  inspector,
  path: '/_inspector',
  server,
  ws: {
    noServer: true,
  },
});

Tracing

Use the trace package for easy application tracing. It works automatically even for nested traces, without passing around any context.

import { Tracer } from '@exotjs/trace';

const tracer = new Tracer();
const { trace } = tracer;

// Now simply use `trace()` anywhere in your code
trace('mytrace', () => {
  // some work...
})

Alternatively, use the inspect.instruments.traces.trace() function.

Contributing

See Contributing Guide and please follow our Code of Conduct.

License

MIT

0.1.0

3 months ago