docs

Routing

In a web application, routing is a process that determines how an application will responds to a particular request.

Routejs provide simple and robust apis for http routing, routejs support object and array based routing, named routing, grouped routing and host based url routing.

Route definition:

router.METHOD("PATH", HANDLER);

Where:

Example:

app.get("/", function (req, res) {
  res.end("GET");
});

app.post("/", function (req, res) {
  res.end("POST");
});

Index

Http methods

Routejs support wide range of http request methods, all supported http request methods are the following:

Example:

// Handle get http request
app.get('/', function (req, res) {
  res.end("GET");
});

// Handle post http request
app.post("/", function (req, res) {
  res.end("POST");
});

// OR

app
  .get('/', function (req, res) {
    res.end("GET");
  })
  .post("/", function (req, res) {
    res.end("POST");
  });

Handle any of the given http request methods:

// Handle get or post http request
app.any(["get", "post"], "/", function (req, res) {
  res.end("Ok");
});

Handle all http request methods:

// Handle all http request
app.all("/", function (req, res) {
  res.end("Ok");
});

Route parameters

Routejs supports route parameters, route parameters are used for dynamic routing.

In routejs we can define route parameters using : syntax, we can put route parameter name after : like :name and we can access them using req.params object.

In route parameter name only alphabet (a-z and A-Z), numbers (0-9) and _ (underscore) is allowed.

Example:

app.get("/user/:name", function (req, res) {
  // Get route parameters using params object
  res.end(req.params.name);
});

It will match everything after /user/ till /, for example:

Routejs also support all valid regular expression in route parameters. But in routejs regex are only allowed inside () brackets.

Regular expression:

app.get("/user/(\\d+)", function (req, res) {
  res.end("Ok");
});

Make sure to escape all regular expression special characters, otherwise you might get wrong result.

Named regular expression:

app.get("/user/:id(\\d+)", function (req, res) {
  res.end(req.params.id);
});

In routejs you can set custom regex for route parameters.

Named routes

Routejs supports named routing, we can assign unique name to url routes, which can be used to generate url using name.

// Set name to route
app.get("/", function (req, res) {
  res.end("Ok");
}).setName("home");

// Get path of named route
console.log(app.route("home"))

Group routes

In routejs we can group related routes together.

Group defination:

router.group("PATH", CALLBACK);

Where:

Example:

// Group related routes
app.group("/blog", function (router) {
  router.use(function (req, res, next) {
    // Group middleware
    next();
  });

  router.get("/read", function (req, res) {
    res.end("Ok");
  });

  router.post("/create", function (req, res) {
    res.end("Ok");
  });
});

// OR

const blog = new Router();
blog
  .use(function (req, res, next) {
    // Group middleware
    next();
  })
  .get("/read", function (req, res) {
    res.end("Ok");
  })
  .post("/create", function (req, res) {
    res.end("Ok");
  });

app.group("/blog", blog);

It will generate the following url routes:

Host routes

Routejs supports host based url routing.

Route defination:

router.domain("HOST", CALLBACK);

Where:

In routejs by default all routes and middlewares are global, which means they are executed on every request. If you are using host based url routing, please make sure to set the default host for all global routes and middlewares, so that they are not executed on every request.

Set default host to all routes and middlewares:

const app = new Router({
  host: "localhost"
});

If we set default request host to all routes and middlewares, then they are only executed when the request host and path both are matched.

Example:

// Route is only executed when request host is localhost
app.get("/", function (req, res) {
  res.end("Ok");
});

// Route is only executed when request host is blog.localhost
app.domain("blog.localhost", function (router) {
  router.get("/", function (req, res) {
    res.end("Blog");
  });
});

Host parameters:

Routejs supports named parameters and regular expression in host based url routing.

app.domain(":name.localhost", function (router) {
  router.get("/", function (req, res) {
    res.end(req.subdomains.name);
  });
});

Array based routing

Routejs is very simple and flexible, it support both object and array based url routing.

Let’s create urls.js urls file for url routes:

const { path, use } = require("@routejs/router");

// Url routes
const urls = [
  path("get", "/", (req, res) => res.end("GET")),
  path("post", "/", (req, res) => res.end("POST")),
  path(["get", "post"], "/blog", (req, res) => res.end("Blog")),
  // Create 404 page not found error
  use((req, res) => res.writeHead(404).end("404 Page Not Found")),
];

module.exports = urls;

Use urls in routejs app:

const { Router } = require("@routejs/router");
const http = require("http");
const urls = require("./urls");

const app = new Router();

// Use url routes
app.use(urls);

http.createServer(app.handler()).listen(3000);

In array based routing path() function is used to set http routes. We can also use all(), use() and domain() functions.