npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-route-recognizer

v0.2.3

Published

Resolve Ember.js routes by path in Node.js

Downloads

173

Readme

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 }