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

page-express-mapper

v2.0.2

Published

Plugin for page.js which directly imitates the Express API.

Downloads

1,083

Readme

page-express-mapper

Build Status codecov npm

A plugin for page.js which aims to provide a direct imitation of the Express API so you can write isomorphic (aka universal, amphibious, etc) router code that can be shared on the client and the server with your Express application without modification.

With this plugin you should be able to write isomorphic JavaScript apps that maximize code reuse on the client and the server, so long as you run Express on the server, page.js on the client, and use a JS-based templating system that can run on the client and server.

This module was built and is maintained by the Roosevelt web framework team, but it can be used independently of Roosevelt as well.

Usage

  • Add page-express-mapper to your npm dependencies.
  • Load page-express-mapper.js into your frontend JS bundle along with page.js.
  • Then initialize it by calling pageExpressMapper() before defining any routes:

Assuming your server code begins something like:

// express-server.js

// init express
const app = express()

// load an isomorphic routes.js file that declares routes
// you can share this file verbatim with the client
require('routes')(app)

And your client code begins something like:

// client.js

// require dependencies
const page = require('page')
const pageExpressMapper = require('page-express-mapper')

// configure pageExpressMapper
const router = pageExpressMapper({
  renderMethod: function(template, model) {
    // render a template using your
    // favorite templating system here
  }
})


// load the same isomorphic routes.js file as above
// you can share this file verbatim with the server
require('routes')(router)

// init page.js
page()

You can then write identical routes for both sides, such as:

// routes.js

// these routes will be shared on both the client and server
module.exports = function(router) {
  router.route('/someRoute').get(function(req, res) {
    res.render('someTemplate', {some: 'model'})
  })
}

Note: You will also need a way to share your templates with the client and server as well. If you're using Roosevelt, you can use the clientViews feature. If you're using vanilla Express, you'll need to create a build script that will allow the template files to be shared on both sides.

Params

pageExpressMapper() returns a router object to attach routes to (like Express) and accepts the following params:

function renderMethod(template, model)

Required

This is designed to mimic the Express render method. Load your own templating system elsewhere in your app and call its render method here as a function of your own design.

As with Express, the template argument should refer to a named template and the model argument should be a JSON string or JavaScript object.

For example, using the Teddy templating engine:

pageExpressMapper({
  renderMethod: function(template, model) {
    const newHTML = teddy.render(template, model)
    // do something with the new HTML
  }
})

This should work with any templating engine which supports both client-side rendering and Express on the server-side.

object customRouter

Optional

By default this plugin matches the Express 4 API, but if you want to remap it, supply a customRouter. For example, to match the Express 3 API, you could do this:

pageExpressMapper({
  renderMethod: someRenderMethod,
  customRouter: {
    get: function(route, callback) {
      page(route, callback)
    },
    post: function(route, callback) {
      page(route, callback)
    },
    all: function(route, callback) {
      page(route, callback)
    }
  }
})

Default: undefined

The router object returned by pageExpressMapper() also has a member object called stack indexed by route with member booleans for whether the route accepts GET, POST, or both. This is useful for getting a list of all registered routes on your frontend.

Example router.stack object:

{
  "/": {
    "get": true
  },
  "/about": {
    "get": true
  },
  "/pageWithForm": {
    "get": true,
    "post": true
  }
}

Sample app

  • Clone this repo.
  • Install dependencies.
  • cd to your clone.
  • npm run sample-app.
  • Open the browser dev tools and examine the console logs when you click the links in the sample app to see evidence of it working.