prouty
v0.0.5
Published
tiny, yet powerful, promise based router for node
Downloads
3
Readme
Prouty is a minimal but powerful node request router.
Example
See example.mjs for a full example.
import http from 'http';
import {router, Response, respond} from 'prouty';
const rtr = router();
rtr.param('greeting', (req, greeting) => greeting);
rtr.get('helloWorld', () => 'Hello world!'); // JSON response by default!
rtr.get(':greeting/:name', ({params: {greeting, name}}) => new Response({
headers: {'Content-Type': 'text/plain'},
body: `${greeting} ${name}`,
}));
http
.createServer(async (req, res) => respond(res, await rtr(req)))
.listen(8080, () => console.log('Server running on port 8080'));
Why
Why yet another router? Simplicity!
You use it, you own it. If you use a package in your software, you have to take care of it. You have to keep up with security issues and so on. Prouty is so dead simple and free of further dependencies, that using it in your project is very low risk. You can easily understand the hundred or so lines of its code.
Leveraging Promises, Prouty allows you to just return the results from your (async) request handlers. This makes your code simpler, too, and easier to read.
In spite of its simplicity, Prouty is pretty powerful and capable of elegantly structuring your routing. It mostly looks like the express router but uses promise semantics for most response generation. You can also handle the response yourself, though - just inherit from Response and or use the passed node-response object and return true, or whatever ...
I use node mostly for writing JSON-API services. So I wanted a trivial router that simplifies my life there and Prouty has proven to be this router. I encourage you to throw away the Response and error handling stuff I did, just use the trivial router.mjs and adapt it to your needs.
I hope, this is of some use to you.