server-base-router

raw JSON →
7.1.32 verified Mon Apr 27 auth: no javascript

A router plugin for the server-base framework, built on http-hash. Provides route definition, middleware support via @setup context.use(), and request/response helpers (req.json, res.notFound, res.error). Integrates with mime for content-type negotiation. Version 7.1.32 is current, with stable release cadence. Key differentiators: generator/async route support, splat params, and environment-based MIME configuration (MIME_TYPES, MIME_TYPES_PATH).

error TypeError: router is not a function
cause Incorrect import; missing '/router' subpath
fix
Use const router = require('server-base/router')
error Error: Cannot find module 'server-base-router'
cause Package not installed
fix
npm install server-base-router
gotcha router path must include '/router' or be required directly
fix Use require('server-base/router') or require('server-base-router')
gotcha ESM import not supported; module is CommonJS only
fix Use require() instead of import
breaking Route definition structure changed in v6? (check changelog)
fix Review migration guide for breaking changes
npm install server-base-router
yarn add server-base-router
pnpm add server-base-router

Demonstrates basic routing, middleware via @setup context.use(), route params, async handler, and request/response helpers.

const http = require('http');
const router = require('server-base/router');
const routes = router({
  '@setup': (ctx) => {
    ctx.use((req, res, next) => {
      console.log('middleware');
      next();
    });
  },
  '/hello/:name': (req, res, params) => {
    res.text('Hello ' + params.name);
  },
  '/api/data': {
    async get(req, res) {
      const data = await req.json();
      res.json({ received: data });
    }
  }
});
const server = http.createServer(routes);
server.listen(3000, () => console.log('Server running on 3000'));