@bayerjs/router
TypeScript icon, indicating that this package has built-in type declarations

0.3.0-alpha.1 • Public • Published

@bayerjs/router

Package containing a router middleware for the Bayer.js server library.

const server = new Bayer();
const router = new Router();
router.route("/profile/:name", function(params) {
  console.log(params);
  return `Hello, ${params[0]}!`;
});

server.use(router.middleware(), 1);
// tslint:disable-next-line:no-console
server.listen(3000).then(() => console.log("Listening on http://localhost:3000"));

The router makes it easier to create practical web servers. Instead of directly exposing the Request and Response objects, the Router middleware will expose extracted data directly, tucking the req/res objects behind the request object.

Usage

Add routes

The following code snippets are equivalent:

const router = new Router();
router.route("GET", "/path/to/route", function() {
    return "Hello World!";
});

Routes can have parameters, prefixed by a colon character:

router.route("GET", "/users/:user", userDetail);

function userDetail({ params }) {
  const { firstname, lastname, email } = getUserFromSlug(params[0]);
  return {
    content: JSON.stringify({ firstname, lastname, email }),
    statusCode: 200
  };
}

The params array contains the parsed values of the parameters. Optional parameters can be suffixed with a question mark.

router.route("GET", "/posts/:post?", postDetail)
function postDetail(props) { /* ... */ }

Middlewares (To be implemented)

Router can have their own middlewares. They will be applied before any route. To add more data through the middleware, use the extra object, which is the same as the extra object of the Server Middleware.

router.use(tap({ path, extra }) => {
  if(path.startsWith("/admin")) {
    if(!checkUserIsAdmin(extra.user.id)) {
      throw new HttpError(403, "Unauthorized", "You are not authorized to access the administration dashboard.");
    }
  }
});

Package Sidebar

Install

npm i @bayerjs/router

Weekly Downloads

2

Version

0.3.0-alpha.1

License

MIT

Unpacked Size

18.6 kB

Total Files

7

Last publish

Collaborators

  • solarliner