GithubHelp home page GithubHelp logo

velocy's Introduction

Velocy

A blazing fast, minimal backend framework for Node.js

โš ๏ธ Live stream: We're now coding/building this framework live on YouTube!

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:

Tree

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"]}

velocy's People

Contributors

ishtms avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.