
Security News
Feross on Risky Business Weekly Podcast: npm’s Ongoing Supply Chain Attacks
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
express-api-helper
Advanced tools
Simple API helper module for Express apps.
ok(req, res, data)
Respond with 200 OK
and JSON-encoded data.
req
express Requestres
express Responsedata
ObjectbadRequest(req, res, errors)
Respond with 400 Bad Request
and JSON-encoded error object, {message:String,errors:Array}
.
req
express Requestres
express Responsedata
Array (of String) or Stringunauthorized(req, res)
Respond with 401 Unauthorized
and JSON-encoded error object, {message:String}
.
req
express Requestres
express Responseforbidden(req, res)
Respond with 403 Forbidden
and JSON-encoded error object, {message:String}
.
req
express Requestres
express ResponsenotFound(req, res)
Respond with 404 Not Found
and JSON-encoded error object, {message:String}
.
req
express Requestres
express ResponseunsupportedAction(req, res)
Respond with 405 Method Not Allowed
and JSON-encoded error object, {message:String}
.
req
express Requestres
express Responseinvalid(req, res, errors)
Respond with 422 Unprocessable Entity
and JSON-encoded error object, {message:String,errors:Array}
.
req
express Requestres
express Responseerrors
Array (of String) or StringserverError(req, res, error)
Respond with 500 Internal Server Error
and JSON-encoded error object, {message:String,error:Object}
.
req
express Requestres
express Responseerror
ObjectrequireParams(req, res, params, callback)
Require that listed parameters are present. Checks for presence of each parameter in req.body
object if using express.bodyParser
middleware; otherwise checks for presence of each parameter in req.params
or req.query
. If any parameters are missing, invokes badRequest
with an array of error messages with the form "Missing required parameter: %s"
.
req
express Requestres
express Responseparams
Array (of String) or Stringcallback(err)
FunctionrequireHeaders(req, res, headers, callback)
Require that listed headers are present. Checks for presence of each header in req.headers
. If any parameters are missing, invokes badRequest
with an array of error messages with the form "Missing required header parameter: %s"
.
req
express Requestres
express Responseheaders
Array (of String) of Stringcallback(err)
FunctionSample usage:
var http = require('http'),
express = require('express'),
bodyParser = require('body-parser'),
api = require('express-api-helper'),
app = express(),
Post = require('./models/post');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.all('/api/*', function (req, res, next) {
if (!req.user) return api.unauthorized(req, res);
next();
});
app.post('/api/posts', function (req, res) {
api.requireParams(req, res, ['title', 'content', 'authorId'], function (err) {
if (err) return api.serverError(req, res, err);
var payload = {
title: req.body.title,
content: req.body.content,
authorId: req.body.authorId
};
Post.create(payload, function (err, post) {
if (err) return api.serverError(req, res, err);
api.ok(req, res, post.toJSON());
});
});
});
app.get('/api/posts', function (req, res) {
Post.find({}, function (err, posts) {
if (err) return api.serverError(req, res, err);
api.ok(req, res, posts.toJSON());
});
});
app.get('/api/posts/:id', function (req, res) {
Post.findById(req.params.id, function (err, post) {
if (err) return api.serverError(req, res, err);
if (!post) return api.notFound(req, res);
api.ok(req, res, post.toJSON());
})
});
http.createServer(app).listen(3000, function () {
console.log("Express API listening on 3000");
});
To run the tests, clone the repository and install the dev dependencies:
git clone git://github.com/paambaati/express-api-helper.git
cd express-api-helper && npm install
make test
0.0.5 (2015-07-17)
403
Fobidden response.FAQs
Simple API helper module for Express apps.
The npm package express-api-helper receives a total of 7 weekly downloads. As such, express-api-helper popularity was classified as not popular.
We found that express-api-helper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.