0.0.2 • Published 7 years ago
jscomponent-router v0.0.2
Router for JS Component
JsComponent-like module which provides a function that lets define your API routes through a component-based array
Basic example:
injectRoutes(router, [
{path: "", component: IndexComponent}
];
// This will map the url "/" to the IndexComponent of your application
Complete example using ExpressJS:
const { injectRoutes } = require("jscomponent-router");
const express = require("express");
const router = express();
class IndexComponent {
route(req, res) {
res.end("Route URL: /");
}
}
class HomeComponent {
route(req, res) {
res.end("Route URL: /home");
}
}
class EditProfileComponent {
route(req, res) {
res.end("Route URL: /profile\nMethod: POST");
}
}
injectRoutes(router, [
{
path: "",
component: IndexComponent,
children: [
{
path: "home",
component: HomeComponent
}
]
},
{
path: "edit-profile",
method: "POST",
component: EditProfileComponent
}
]);
router.listen(8080); // Run the Express server by listening on the port 8080
/* We essentially defined 3 routes for this application.
GET /
GET /home
POST /edit-profile
*/
API
injectRoutes(router, routesList)
Description: Inject a list of routes with their associated component into the router (compatible with Express/KoaJS and similar routing modules)
Params:
{any} router = It depends on your routing system. E.g. in KoaJS this will be the router created using koa-router.
{array} routes. List of routes to map your API.
Route type: {path: "/user", method: "get", component: YourComponent, children: []}.
*method* accepts any HTTP method that your router system supports (get, post, push, head, etc..)
*component* is a single component or array of components, that have the 'route' method.
*children* paths are relative to the parent route. E.g. {path: "user", children: [{path: "profile"}]}; The "/profile" child route will actually map to "/user/profile".
Note: you can assign the 'jscPrefix' property to your router object, which will be prefixed to all paths
Note2: you can assign a function to the 'jscAdapter' property of your router object, that will receive some route properties. Your adapter will manually call the method of your router that map a route. Useful when jscomponent-router is not compatibile with your routing system
Tests
npm test
License
MIT