@amatsagu/routify v1.1.2
Routify
Zero dependency, simple HTTP router with near native performance.
npm install @amatsagu/routify
Features
- 📦 Light weight (7,92kb)
- ⚡ Extremely high performance (99.94% of Node HTTP module)
- ⚓ Easy applicable. It can be attached to HTTP server or its modification.
- 🧱 Provides basic layer of abstraction.
- Static & dynamic routing (customizable)
Basic, middlewares(temporarily dropped)
Example usage
import { createRouter } from "@amatsagu/routify";
import { createServer } from "http";
const port = process.env.PORT;
const router = createRouter();
const server = createServer(router.dynamicHandler);
router.get("/", (req, res) => {
res.sendFile("./index.html");
});
router.get("/add/:a/:b", (req, res) => {
const {a, b} = req.params;
if (!a || !b) res.sendText("Invalid request", 500); // Defined status code
else res.sendText(`${a} + ${b} = ${a + b}`);
});
server.listen(port, () => console.log(`Service is working using port: ${port}`));
You can also call Router class to achieve the same. Let's use static routing this time. StaticHandler doesn't scan for url params but is faster.
import { Router } from "@amatsagu/routify";
import { createServer } from "http";
const router = new Router();
createServer(router.staticHandler).listen(process.env.PORT);
router.get("/user", (req, res) => {
res.sendJSON({
username: "Amatsagu",
age: 18
});
});
API
Routify focuses on effectiveness so we keep nearly everything default. Those few changes are here to make working with http more pleasure.
Request extends default http.IncomingMessage
- Comes with fixed TypeScript typings.
- Added
params
(object) for route arguments. - Added
body
which can be either undefined, string or object depending on options.- Router constructor accepts 2 options:
export interface RouterOptions {
waitForBody: boolean;
parseBody: boolean;
}
Response extends default http.ServerResponse and received 3 handy methods:
sendText(data, statusCode?)
for quick responding with utf8 data.sendJSON(payload, statusCode?)
for safe, quick sending JSON objects.sendFile(pathToFile, statusCode?)
for sending files. There's support for common mime types:html, jpeg, jpg, png, svg, json, js, css, txt
.
Why?
Routify is near express when it comes to functionality but it's much quicker and doesn't produce much overhead. It's made for people who wan't to make small REST APIs or host small websites without need of downloading any of those huge web frameworks.
You can choose between static & dynamic routing which also affect performance. Dynamic can use route params (known from big frameworks) but is a bit slower than static routing (~5%).
Additionally - Unlikely those big frameworks, Routify doesn't try to replace http module but to cooperate with it. All packages targeting http server can work alongside.
#TODO
- Provide some lighweight & handy system for middlewares.
- Improve dynamic route detecing? (make it faster)
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D