velocy
v0.0.14
Published
A blazing fast, minimal backend framework for Node.js
Downloads
34
Readme
Velocy
A blazing fast, minimal backend framework for Node.js Install the package
npm i velocy
# or
yarn add velocy
Import the Router
class, and the createServer
utility -
const { Router, createServer } = require("velocy");
Initialize the router
const router = new Router();
Register an endpoint
router.get("/", (req, res) => res.end("Hello, world!"));
// Or add dynamic parameters
router.get("/api/:version/user/:userId", (req, res) => {
const { version, userId } = req.params;
res.end(`API version: ${version}, User ID: ${userId}`);
});
// add a catch-all route
router.get("*", (req, res) => res.end("404 Not Found"));
Start the server
createServer(router).listen(3000);
Print the entire route tree (for debugging only):
- Shows the name of the function if a named function is passed as a callback
- Shows the function body trimmed down to 30 characters if used an anonymous function
router.get("/", function base_route(req, res) {});
router.get("/:id", function get_base_route_id(req, res) {});
router.get("/nope", function get_base_route_id(req, res) {});
router.post("/submit", function post_base_route_submit(req, res) {});
router.get("/hello/:name", (req, res) => {});
router.get("/hello/world", function get_hello_world(req, res) {});
router.post("/hello/world", function post_hello_world(req, res) {});
router.printTree();
The tree output:
Merge routers
const { Router, createServer } = require("velocy");
function getUserList(req, res) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end("/users: " + JSON.stringify({ users: ["Ishtmeet", "Jon"] }));
}
function showUserInfo(req, res) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end("/users/:id: " + JSON.stringify({ user: req.extractedParams.id }));
}
function teamsList(req, res) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end("/teams: " + JSON.stringify({ teams: ["Team Red", "Team Blue"] }));
}
const base_routes = new Router().get("/", (req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello World");
});
const user_routes = new Router().get("/users", getUserList).get("/users/:id", showUserInfo);
const team_routes = new Router().get("/teams", teamsList);
const main_router = new Router();
main_router.merge(user_routes);
main_router.merge(team_routes);
main_router.merge(base_routes);
// Response
// GET / -> Hello world
// GET /users -> {"users":["Ishtmeet","Jon"]}
// GET /users/:id -> {"user":"1"}
// GET /teams -> {"teams":["Team Red","Team Blue"]}
Nest routers
const { Router, createServer } = require("velocy");
function getUserList(req, res) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end("/users: " + JSON.stringify({ users: ["Ishtmeet", "Jon"] }));
}
function showUserInfo(req, res) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end("/users/:id: " + JSON.stringify({ user: req.extractedParams.id }));
}
function teamsList(req, res) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end("/teams: " + JSON.stringify({ teams: ["Team Red", "Team Blue"] }));
}
const base_routes = new Router().get("/", (req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello World");
});
const user_routes = new Router().get("/users", getUserList).get("/users/:id", showUserInfo);
const team_routes = new Router().get("/teams", teamsList);
const main_router = new Router();
main_router.merge(user_routes);
main_router.merge(team_routes);
main_router.merge(base_routes);
const api_router = new Router();
api_router.nest("/api/v1", main_router);
createServer(api_router).listen(3000, () => {
console.log("Server is running on port 3000");
});
// Response
// GET /api/v1 -> 404 Not Found
// GET /api/v1/ -> Hello world
// GET /api/v1/users -> {"users":["Ishtmeet","Jon"]}
// GET /api/v1/users/:id -> {"user":"1"}
// GET /api/v1/teams -> {"teams":["Team Red","Team Blue"]}