A collection of examples for spirit
Some of the examples are in other repositories, but they are all categorized here.
Content
The example shows 3 ways of sending a file from three different routes. All of the routes are essentially the same except for the last route that uses fs.readFile
, since that returns a Buffer. While the 2 other routes return a stream. Note, that this is for serving a file from a route, if you wanted to have a route that serves static files, use resources API.
Most template libraries like jade already export a compile function. So writing a function that calls it and sets up some sort of caching is enough. Now every route can call that function, and since compile returns a string, you can just return that string from the route. There's nothing magical going on here.
The caching is very basic and has no idea of time / diffing. (There is probably a module on NPM that sets up caching and compiling, and is template agnostic.)
The example takes the socket.io chat example as part of socket.io's tutorial, and uses spirit and spirit-router instead of Express.
All these examples use spirit-express.
NOTE: spirit-express does not only support the examples below. It can support most Express middleware, the below are just examples written.
The body-parser module. The example sets up a single POST / route, that will parse the request body. Use curl ex: curl --data "a=123&b=hello" http://localhost:3009/
to see the data outputted back as JSON.
For cookie-parser module. The example shows a a single GET / route that parses any cookies sent in the request. To see the example, you would have to modify your browser to send a cookie, or use curl ex: curl http://localhost:3009 --cookie "Cho=Kim;Greet=Hello
to see the cookie outputted back.
For express-session module. The example shows a simple counter that incremenets on every GET / request. The counter uses cookie storage, so it persists.
For multer module. The example sets a route that delivers a form, and another route that handles POST from that form. When the POST route is triggered, it just returns back the form data as JSON.
For passport module. In the example it uses LocalStrategy to auth, the auth is very basic as it's just an example. But the idea of how to set up passport is shown.
See Server rendering in React, Redux section.
This example comes from Redux's example Universal. It changes the use of Express to spirit with very little modification, webpack -dev and -hot middleware are wrapped with spirit-express to work.