REST server library with dependency injection
Installation
npm install molded
Usage
Molded attempts to be Connect middleware compatible. If you find something that isn't, please create an issue.
With Molded, you define route handlers similar to the ones defined in Express.
var molded = ;var app = ; // send() automatically sends objects as JSONapp; // Use sendJson() to always send JSON, regardless of data typeapp; app;
You can plug in Connect middleware like body-parser.
var bodyParser = ;var molded = ; var users = ; var app = ; app; app; app; app;
Dependency Injection
The primary advantage of Molded is that it makes it easy to inject dependencies into route handlers.
The following examples illustrate some of the things that can be done with dependency injection.
app.value(name, val)
If you want to provide a fixed value to your app, you can use app.value('name', val)
For example, to hardcode a list of users:
appvalue'users' 'user1': username: 'user1' email: 'user1@example.com' 'user2': username: 'user2' email: 'user2@example.com' ;
app.singleton(name, func)
If you want to provide a hardcoded value, but need to inject other dependencies, use app.singleton('name', func)
Singletons are only called once, during app setup.
For example, to inject user1 from the previous example:
app; app;
As a more practical example, consider the following Mongoose database setup.
var bodyParser = ;var q = ;var mongoose = ;var connectionString = 'mongodb://localhost/test';mongoose; appvalue'config' db: mongoose dbConnectionString: connectionString ; app; app; app; app; app; app;
app.provide(name, func)
If you need to inject a custom value for each request, use app.provide('name', func)
For example, to look up a user based on an app route:
app; app;
Error Handling
Error handling in Molded is slightly different than Express. To catch errors, use app.error()
.
Calling app.error('/route', handler)
only handles errors from that route.
Calling app.error(handler)
handles errors for all routes.
// If a provider throws an exception, it is sent to the error handlers matching the request's route.app;app; // If an error handler fails, the exception from the error handler is// passed on to the next error handlerapp;app; // If a route handler throws an exception, it's sent to the error handler(s) that match the routeapp; // Use next(err) for things like async callbacksapp; app;
Promises
In Molded, promises are first-class citizens. Providers that need to make async calls can wrap them in promises. Molded will wait until the promise resolves before passing the result on to whatever depends on that provider.
If a promise throws an exception, Molded passes the exception to the app's error handler. If no error handler is present, Molded sends a 500 with a generic error message.
The following example shows how to provide a simple MongoDB backed API using Mongoose.
Notice that the API endpoints use q.ninvoke
to call the Mongoose methods.
By returning this promise from the handler, we let Molded worry about handling any errors returned by Mongoose.
var q = ;var molded = ;var app = ; var bodyParser = ;var mongoose = ; appvalue'config' dbConnectionString: 'mongodb://localhost/test' ; app; app; app; app; app; app;