simple-route
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

Simple Route for node express

Install

npm install simple-route

Create Object

import { Route, RequestHandler } from "simple-route";

const auth: RequestHandler = (req, res, next) => {
    console.log("Auth middleware");
    if (next) return next()
}

const token: RequestHandler = (req, res, next) => {
    console.log("Token middleware");
    if (next) return next()
}

const route = Route.create({
    path: "/user",
    method: "GET",
    middlewares: [auth],
    func(req, res) {
        res.send("This is user page")
    },
    childs: [
        {
            path: "/:userid",
            method: "GET",
            middlewares: [token],
            func(req, res){
                res.send(`userid is ${req.params.userid}`);
            }
        }
    ]
})

Use

import express, { Application } from "express";
const app: Application = express();

route.use(app, "/api/v1");
const port = 3000;
app.listen(port, () => {
    console.log(`app started on port ${port}`)
});

Response

This is user page

Console

Middleware Auth

Response

userid is simpledev0042

Console

Middleware Auth
Middleware token

Package Sidebar

Install

npm i simple-route

Weekly Downloads

5

Version

1.0.6

License

ISC

Unpacked Size

18.1 kB

Total Files

6

Last publish

Collaborators

  • simpledev0042