ember-route-recognizer
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:
;; const Router = EmberRouter; 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 { this;};
Then our app/router.js
becomes:
;;; const Router = EmberRouter; 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:
moduleexports = ;
Now, we can finally write the code to import my-app
and consume this file:
;; let routeResolver = ; let result = ; console;
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