All Projects → yoshuawuyts → Server Render

yoshuawuyts / Server Render

Licence: mit
HTML server rendering middleware

Programming Languages

javascript
184084 projects - #8 most used programming language

server-render stability

npm version build status test coverage downloads js-standard-style

HTML server rendering middleware. Detects if an incoming request is requesting text/html and calls a function to render the corresponding response.

Usage

Assuming the client is a choo app:

var render = require('server-render')
var merry = require('merry')
var client = require('./client')

var app = merry()
app.use(render(function (route) {
  return client.toString(route))
})
app.start()

Caching

Sometimes you know the paths you're going to render up front, and want to cache them in a Node buffer so the reponse times are as fast as they can be.

var cache = require('server-render/cache')
var render = require('server-render')
var merry = require('merry')
var client = require('./client')

var routes = {
  default: '/404',
  routes: [ '/', '/bar', '/bar/baz', '/bar/:foobar' ]
}

var app = merry()
app.use(cache(routes, function (route) {
  return client.toString(route))
})
app.start()

API

middleware = render(handler(route))

Create a new render function that takes a callback and returns a middleware function. The callback should return a string synchronously. The middleware function has the signature of (req, res, next).

middleware = cache(routes, handler(route))

Cache routes into Node buffers. Takes an object containing a .routes array of routes, and a .default value for the default route to match if no other routes could be matched.

Installation

$ npm install server-render

See Also

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].