GithubHelp home page GithubHelp logo

nvdnkpr / express-shared-routes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hrajchert/express-shared-routes

0.0 3.0 0.0 197 KB

Named routes for the express framework that are shared between the server and the browser

License: MIT License

express-shared-routes's Introduction

Express Shared Routes

Named routes for the express framework that are shared between the server and the browser.

This is a minimalistic library (around 200 lines of code) that allows you to softcode your routes and ease the creation of navigation components like menus and breadcrumbs. The routes are normally defined in the server and can be exported to the browser (if you need client side rendering of links). The library has no dependencies in the browser, weights 370 Bytes gziped and is coded using a UMD pattern, so you can use it directly or with RequireJS.

Name your routes

So, your routes probably look something like this

// Normal express Route
app.get('/hello', function(req, res){
    res.send('hello world');
});

The library allows you to add a name to your routes, so you can easily reference them.

// Our way
routes.get({name: "hello", re: '/hello'}, function(req, res){
    res.send('hello world');
});

The routes are Javascript Object Literals, that must have at least a name, and a regular expresion (re).

Create links

Instead of using hardcoded links like this var href = '/hello/' + name, you can reference a named route, and pass parameters to it. The parameter definition comes from the express route definition.

routes.get({name: "named-hello", re: '/hello/:name'}, function(req, res){
    // Soft coded link
    var href = routes.getLink ('named-hello', {name: 'Handsome'});

    var response = "Hello " + req.param('name');
    response += " <a href='"+ href + "'>Link</a>";
    res.send(response);
});

Notice that getLink is using the named route, so if we change the route's regular expression to something like "/sayhello/:name", all the links will reflect the change.

Add hierarchy

URL's are hierarchal by nature, so are your routes.

Suposse you have an admin page that allows you to list and edit users, you probably have the following URL's

  • /admin: Dashboard of the admin
  • /admin/user: A list of users with the possible actions
  • /admin/user/:id/edit: Edit user form

We can define that structure using the routes:

routes.get({
    name: "admin",
    re: '/admin',
    handler: function(req, res){
        res.send('Admin dashboard');
    }
});

routes.get({
    name: "admin_user_list",
    re: '/user',
    parent: routes.getRoute('admin'),
    handler: function(req, res){
        res.send('List of users');
    }
});

routes.get({
    name: "admin_user_edit",
    re: '/:id/edit',
    parent: routes.getRoute('admin_user_list'),
    handler: function(req, res){
        res.send('Edit form');
    }
});

The parent property is the parent Route (a Javascript Object Literal), and indicates that our re depends on our ancestors.

In here we also show another way to define the route handler. Instead of adding it after the Javascript Object (the Route), we add it as a property.

Take it for a spin

You got this far and you are still interested? Check out how to install and bootstrap the library with this Basic Example.

Once you learn the basics, check out how to create a menu bar and share your routes with the client. Are you using RequireJS? Here is the same example using AMD loader.

See how to create complex navigation like this Breadcrumb example.

Whats comming?

aka: TODO

  • Add an example of how this plays with Backbone to do a Single Page Application
  • Add an example of how it can be used to build a modular MVC express app
  • Change the parent property to allow to use a string (the name of the route) instead of having to pass the Object Literal
  • Add the ability to override a rule (could be useful for MVC)
  • Maybe add prototype inheritance to the routes so it take less code to do some navigation tools

express-shared-routes's People

Contributors

hrajchert avatar

Watchers

Navid Nikpour avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.