
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
*** WORK IN PROGRESS ***
This is a work in progress, but should be mostly stable. The API is subject to change.
Baucis is Express middleware for automatically creating REST services from Mongoose schemata.
Like Baucis and Philemon of old, this library provides REST to the weary traveler. The goal is to create a JSON REST API for Mongoose that matches as closely as possible the richness and versatility of the HTTP 1.1 protocol.
David Rijckaert - Philemon and Baucis Giving Hospitality to Jupiter and Mercury
An example of creating a REST API from a Mongoose schema:
// Define a Mongoose schema
var Vegetable = new mongoose.Schema({
name: String
});
mongoose.model('vegetable', Vegetable);
// Create routes for the schema
baucis.rest({
singular: 'vegetable'
});
// Create the app and listen for API requests
var app = express();
app.use('/api/v1', baucis());
app.listen(80);
Later, make requests:
GET /api/v1/vegetables/:id — get the addressed document
PUT /api/v1/vegetables/:id — create or update the addressed document
DEL /api/v1/vegetables/:id — delete the addressed object
GET /api/v1/vegetables — get all documents
POST /api/v1/vegetables — creates a new document and sends back its ID
PUT /api/v1/vegetables — replace all documents with given new documents
DEL /api/v1/vegetables — delete all documents
baucis.rest
returns an instance of the controller created to handle the schema's routes.
var controller = baucis.rest({
singular: 'foo'
});
var subcontroller = baucis.rest({
singular: 'bar',
publish: false, // don't add routes automatically
restrict: function (query, request) { // allows direct access to the Mongoose queries
query.where({ parent: request.params.fooId });
}
});
// Embed the subcontroller at /foos/:fooId/bars
controller.use('/:fooId/bars', subcontroller);
// Embed arbitrary middleware at /foos/:fooId/qux
controller.use('/:fooId/qux', function (request, response, next) {
// Do something cool…
next();
});
Controllers are Express apps, so do whatever you want with them.
var controller = baucis.rest({
singular: 'robot'
});
controller.use(function () { ... });
controller.set('some option name', 'value');
controller.listen(3000);
Baucis uses the power of Express, without getting in its way. It's meant to be a way to organize your REST API's Express middleware.
Requests to the collection (not its members) take standard MongoDB query parameters to filter the documents based on custom criteria.
Examples with jQuery:
$.getJSON('/api/v1/vegetables/4f4028e6e5139bf4e472cca1', function (data) {
console.log(data);
});
$.ajax({
type: 'POST',
dataType: 'json',
url: '/api/v1/vegetables',
data: { name: 'Potato' }
}).done(function (vegetable) {
console.dir(vegetable);
});
An example with Backbone:
var Foos = Backbone.Collection.extend({
url: '/foos'
});
var Bar = Backbone.Model.extend({
urlRoot: '/bars'
});
Use plain old Connect/Express middleware, including pre-existing modules like passport
. For example, set the all
option to add middleware to be called before all the model's API routes.
baucis.rest({
singular: 'vegetable',
all: function (request, response, next) {
if (request.isAuthenticated()) return next();
return response.send(401);
}
});
Or, set some middleware for specific HTTP verbs:
baucis.rest({
singular: 'vegetable',
get: [middleware1, middleware2],
post: middleware3,
del: [middleware4, middleware5]
});
© 2012-2013 William P. Riley-Land
FAQs
Build scalable REST APIs using the open source tools and standards you already know.
The npm package baucis receives a total of 10 weekly downloads. As such, baucis popularity was classified as not popular.
We found that baucis demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.