Totemish routing is a wrapper for Node.js native HTTP server. It doesn't use any magic wands and lets you go all the
tough way through Node.js jungle. At its core, Totemish routing is built upon middleware that does exactly what you
expect. You have many entry points to put your middleware to get desired results. All the middleware is then put into
a pipeline that is executed on Node.js HTTP server's request and response. Routing in Totemish is based on using RegExp
which gives you incredible control over the routing as well as incredible pain in whatever applicable. Totemish routing
only relies on dependency-free @totemish/core
.
npm i --save @totemish/routing
import { AppFactory, Router } from '@totemish/routing';
/**
* Simple middleware to log incoming requests.
*/
const Logger = (ctx) => {
console.log(`${ctx.req.method}: ${ctx.req.url}`);
return ctx;
}
/**
* Initialize router. If you want to dive right in, use Router#empty.
*/
const router = Router.empty();
/**
* Tell the router that in has to use Logger middleware on all the nested routes.
*/
router.middleware(Logger, (r) => {
/*
* Here you get another router that will be merged to the parent router as soon as you return it.
*/
r.get('/', (ctx) => ctx.res.end('Hello from the index page!');
r.get('/about', (ctx) => ctx.res.end('Under construction');
return r;
});
/**
* Create an app that exposes our router.
*/
const app = AppFactory.create(router);
/**
* Start the app by forcing it to listen to given port.
*/
app.listen(8000);
console.log('Server running on port 8000');