
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
api-builder
Advanced tools
Build API's in Node.
Load controllers, models and structures into optionally namespaced target(s).
Initialize routes from config/routes.js.
Easily add Redis backed sessions and gzipped Redis response caching.
Install the module with: npm install api-builder
var apiBuilder = require('api-builder');
var express = require('express');
app = express();
app.use(express.cookieParser());
Use Redis for sessions
Requires a config/redis-session.yml, something like :
defaults: &defaults
db: 2
host: localhost
port: 6379
development:
<<: *defaults
secret: qwertyuiop-dev:-)
production:
<<: *defaults
secret: qwertyuiop-prod:-)
then in your server.js
apiBuilder.redisSession(app, express);
Optionally cache responses in Redis, to be defined per controller function.
See sample controllers below for usage.
Requires a config/redis-cache.yml, something like :
defaults: &defaults
host: localhost
port: 6379
development:
<<: *defaults
password: redis
production:
<<: *defaults
password: hard-password
then in your server.js say
apiBuilder.cache.init(app, express);
Now some default Express stuff
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));
Make models available from the top level global namespace
apiBuilder.models.load('app/models', global);
Controllers namespaced by "controllers", eg. controllers.blah.blah
apiBuilder.controllers.load('app/controllers', global.controllers = {});
Now you can connect some defined routes to your controller functions
apiBuilder.routes.load(app, 'config/routes.yml', global.controllers);
Routes (config/routes.yml) might look like this :
- { path: '/', method: get, action: Home.index }
- { path: '/:slug/:id/blogs', method: get, action: Blogs.show }
- { path: '/users/details', method: get, action: Users.details }
- { path: '/contact-us', method: post, action: Messages.contactUs }
/app/controllers/home.js might look like :
module.exports = {
index: function (req, res) {
res.cache = 3600; // Cache this in Redis for one hour
res.json({
cheese: 'camembert'
});
},
foo: function(req, res) {
// This won't cache because we haven't defined a res.cache lifetime
res.render('some/template', { cheese: 'edam' });
}
};
Given :
/app/models/user.js
/app/models/user/roles.js
/app/models/user/profile.js
loaded as :
apiBuilder.models.load('app/models', global.models = {});
models are then accessed as :
models.User.someAttribute
models.User.Roles.someAttribute
models.User.Profile.someAttribute
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
Copyright (c) 2013 Mark Selby
Licensed under the MIT license.
FAQs
Build JSON API's in Node
The npm package api-builder receives a total of 1 weekly downloads. As such, api-builder popularity was classified as not popular.
We found that api-builder demonstrated a not healthy version release cadence and project activity because the last version was released 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
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.