Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
expressive-api
Advanced tools
You think of an API like a directory structure, so it's time you served it out of one.
Define your api like this:
|-apiDir
|-user
|-fetch.js
|-delete.js
|-create.js
|-deep
|-deeper
|-explore.js
|-mcHammer
|-touch.js
Each endpoint must export a function which accepts requests and produces json-friendly output, like this:
explore.js
module.exports = function explore(req, callback) {
var depth = req.body.depth;
if (depth > 1000) {
return {
balrog: true
};
} else {
return {
balrog: false
}
}
}
For async functions, either works:
callback
param supplied to your endpointHere's how to actually serve the API:
var express = require('express');
var app = express();
// you'll want to be able to parse json and querystrings
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
// serve away
var serveApi = require('expressive-api');
app.use(serveApi('./apiDir'));
app.listen(3000, function () {
console.log('And it's running - easy hey?');
});
Now you can hit your API:
GET /user/fetch
POST /deep/deeper/explore
It's worth mentioning again that this is a JSON only API.
Under the hood, this uses require-directory, so you can pass in any of its options directly like this:
app.use(serveApi('./apiDir'), {
whitelist: /\.js$/
});
You can specify a transformParams(req, callback, res)
function which gets applied to the usual parameters.
The result should be an array of the parameters you want your API endpoints to receive.
Here's one I use:
function transformParams(req, callback, res) {
// combine body and query into the params for the api
var params = Object.assign({}, req.body);
params = Object.assign(params, req.query);
return [params, req.user, callback];
}
app.use(serveApi('./apiDir'), {
transformParams: transformParams
});
Just add an array of allowedMethods
as a property of your endpoint function, like this:
function saveData(req, callback){
// do the saving
}
saveData.allowedMethods = ['POST']
module.exports = saveData;
Ordinarily, whatever object an endpoint's exported function returns, is sent to the client.
However, since res
is passed as an argument too, you can actually respond however you like, overriding the default JSON response.
Your feature suggestions and pull requests are welcome on the repo.
PS: Dear react-redux-universal-hot-example, your sick boilerplate inspired some of what's baked into this. Thank you!
FAQs
Express API serving that just makes sense
The npm package expressive-api receives a total of 15 weekly downloads. As such, expressive-api popularity was classified as not popular.
We found that expressive-api 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.