simple-route
v1.0.6
Published
simple node express route
Downloads
16
Maintainers
Readme
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}`)
});
http://localhost/user
Response
This is user page
Console
Middleware Auth
http://localhost/user/simpledev0042
Response
userid is simpledev0042
Console
Middleware Auth
Middleware token