Pimm
Pimm is an easy-going web framework for Node. Pimm focuses on removing boilerplate code from your applications, letting you tend to the logic and architecture (the fun bits!) of your app.
Highlights:
- Group together request handlers into "controller" modules.
- Run filters before controller methods.
- Render JSON, text, html, and view templates from controllers.
- Easily map routes to controller methods.
- Auto-create CRUD routes in a RESTful pattern.
- Supports 25 different templating engines.
- Deep support for promises.
Example
If you prefer to jump right into the code, check out the example application:
API
Pimm
new Pimm(options)
Creates a new Pimm instance. Accepts an optional configuration object.
var app = dir: __dirname; // you can also set options after instantiationappdir = '/updated/path';
Options
- dir
string
Folder path to load the application from. Default./
. - port
int
The port to listen on for requests. Default3000
. - caching
bool
Enables/disables view caching. Highly recommended to enable in production. Defaulttrue
. - templating
string
The templating language to use for views. See consolidate.js for a list of supported engines. Defaultjade
. - static
string
Folder path to serve static assets from. Disabled by default. - session
string
A secret key to use for encoding sesssion cookies. Disabled by default.
.start()
Starts the application. Returns a promise that resolves when the application has started.
appstart;
.config(name, value?)
Gets/sets an application config value. You can use this as a generic key/value store for shared configuration across your Pimm app.
app; // sets a valueapp'my message'; // get a value
.use(middleware, args?...)
Add a middleware layer to the app. Additional arguments are passed to the middleware
function.
app;
Pimm uses mach under the hood for handling middleware. Take a look at their wiki for examples.
.routes(function)
Runs function
to add routes to the application.
app;
Controller
Every controller module you place in the controllers/
directory will automatically inherit several
APIs for you to use when parsing and responding to requests.
All controller actions will also receive a connection
parameter that can be used to parse
requests and set responses.
conn.json(status, json)
Create a JSON response object. Automatically sets a Content-Type: "application/json"
header.
Optionally accepts a status
as the first parameter.
{ const message = "hello world"; return conn; };
conn.text(status, text)
Create a text response object. Automatically sets a Content-Type: "text/plain"
header.
Optionally accepts a status
as the first parameter.
{ return conntext404 'Not found!'; };
conn.html(status, html)
Create a html response object. Automatically sets a Content-Type: "text/html"
header.
Optionally accepts a status
as the first parameter.
{ return conn; };
conn.send(status, content)
Create a manual response object. Useful if none of the other response helper methods fit your needs.
{ connresponsecontentType = 'text/html'; return conn; };
conn.render(status, view, data)
Creates a response object by rendering a view file. Rather than passing a template to the render method, you pass a string that represents the file path (relative to the views directory) of the view to render.
{ const posts = title: 'Hello world!' title: 'This is my blog' ; return conn; };
conn.redirect(status, location)
Creates a redirect response object. Automatically sets the status to 302
and a Location
header.
{ return conn; };
.before(method..., function)
Wraps a request method(s) with a new function. If the wrapper function returns a value, the underlying method will never get invoked.
{ this } { return conntext'Authed!'; } { return conntext'Authed!'; };