ember-route-recognizer

0.2.3 • Public • Published

ember-route-recognizer

Greenkeeper badge npm version Build Status Build status dependencies Status devDependencies Status

Resolve Ember.js routes by path in Node.js

Installation

yarn add ember-route-recognizer

Motivation

In your Node.js server, you have an incoming request path and need to know what Ember.js route it maps to. It is impossible to guess with just the url what Ember.js route it maps to because a route can have a custom path. We need a way to load the Ember.js route map in Node.js. With some slight modifications to your Ember.js router.js, it is now possible.

Example

Given a router at app/router.js like this:

import Ember from 'ember';
import config from './config/environment';
 
const Router = Ember.Router.extend({
  location: config.locationType,
  rootURL: config.rootURL
});
 
Router.map(function() {
  this.route('parent-route', { path: 'custom-route/:dynamic_segment' }, function() {
    this.route('child-route');
  });
});
 
export default Router;

We must first extract just the route map to a new file because we don't want to import all of Ember.js. Create a new file at app/router-map.js like this:

exports.routerMap = function() {
  this.route('parent-route', { path: 'custom-route/:dynamic_segment' }, function() {
    this.route('child-route');
  });
};

Then our app/router.js becomes:

import Ember from 'ember';
import config from './config/environment';
import { routerMap } from './router-map';
 
const Router = Ember.Router.extend({
  location: config.locationType,
  rootURL: config.rootURL
});
 
Router.map(routerMap);
 
export default Router;

Next, we need to get this new app/router-map.js into our Node.js server. This can be done a variety of ways, but an npm import is the easiest way. Given an Ember.js app named my-app, you would run this in your Node.js server directory:

yarn add git+https://git@my-git-server/my-app.git

This will add my-app to our Node.js server's package.json, but your Ember.js app isn't exporting anything yet. Next, create a file at index.js in your Ember.js app and add this:

module.exports = require('./app/router-map');

Now, we can finally write the code to import my-app and consume this file:

import emberRouteRecognizer from 'ember-route-recognizer';
import { routerMap } from 'my-app';
 
let routeResolver = emberRouteRecognizer(routerMap);
 
let result = routeResolver('/custom-route/some-variable/child-route?someParam=true');
 
console.log(result);

The result structure comes from route-recognizer.

{ '0': 
   { handler: 
      { name: 'child-route',
        fullName: 'parent-route.child-route',
        path: '/child-route',
        fullPath: '/custom-route/:dynamic_segment/child-route' },
     params: 
      { dynamic_segment: 'some-variable' },
     isDynamic: true },
  queryParams: { someParam: 'true' },
  length: 1 }

Readme

Keywords

Package Sidebar

Install

npm i ember-route-recognizer

Weekly Downloads

57

Version

0.2.3

License

MIT

Last publish

Collaborators

  • kellyselden
  • psheraton