
Security News
The Code You Didn't Write Is Still Yours to Defend
AI agents are pulling packages into environments no scanner is watching, creating exposure before security teams can see it.
pixl-restify
Advanced tools
TL;DR: Simplify development of JSON REST services using plain class methods that are shielded from knowledge of the transport (HTTP, encodings, etc.)
This module helps simplify writing REST APIs by inspecting your components for methods matching a naming scheme and creating REST endpoints. It creates HTTP URL handlers for pixl-server-web and dispatches calls to your methods. It is built as a component of pixl-server, a lightweight framework for building node.js daemon applications.
// myservice.js
const Component = require("pixl-server/component");
class MyService extends Component {
constructor() {
super();
this.__name = "MyService";
this.defaultConfig = {
};
this.counters = new Map();
}
validate(method, args) {
return (typeof args.id == "undefined") ? "missing_param" : true;
}
// Handles HTTP GET /myservice/rest/v1.0/Counter?id=blah
async getCounter(args) {
if (this.counters.has(args.id)) {
return {name: args.counter, value: this.counters.get(args.id)}
}
else {
throw {code:'no_counter', message:"No counter defined for " + args.id};
}
}
/*
Handles POST /myservice/rest/v1.0/Counter
{
"id": "blah",
"value": 42
}
*/
async createCounter(args) {
if (this.counters.has(args.id)) {
throw {code:'counter_exists', message:"Counter already created for " + args.id};
}
var val = (typeof args.value != "undefined") ? parseInt(args.value) : 0;
this.counters.set(args.id, val);
return {id: args.id, value: val};
}
/*
Handles Put /myservice/rest/v1.0/Counter
{
"id": "blah"
}
*/
async updateCounter(args) {
if (this.counters.has(args.id)) {
var val = (typeof args.value != "undefined") ? parseInt(args.value) : 1 + this.counters.get(args.id);
this.counters.set(args.id, val);
return {id: args.id, value: this.counters.get(args.id)}
}
else {
throw {code:'no_counter', message:"No counter defined for " + args.id};
}
}
}
var PixlServer = require('pixl-server');
var server = new PixlServer({
__name: 'MyServer',
__version: "0.1",
config: {
"log_dir": "/var/tmp",
"log_filename": "my_test.log",
"debug_level": 9,
"WebServer": {
"http_port": 3080
},
"PixelRestify" : {
"endpoints": [
{
"name": "MyService",
"regex": "/myservice/rest/v1.0/(\\w+)"
}
]
}
},
components: [
require('pixl-server-web'),
require('./my_service.js'),
require('pixl-restify')
]
});
server.startup( function() {
console.log("Main startup");
});
The default mapping from component method to HTTP methods is defined below and can be overriden in the configuration.
this.defaultConfig = {
"methodMap": {
"get" : "get",
"create" : "post",
"update" : "put",
"delete" : "delete"
}
};
For example if you name your method "updateMyWidget", the PixlRestify will route HTTP PUT requests for /MyWidget to your method.
Your method is passed args, serverArgs where
Your method can return any of the following:
If your method throws an exception PixlRestify will try to find a code element and a message in the thrown object and return a JSON repsonse in the format of {code:"error_code", message:"Error details"}. It will also log the error using pixl-logger with a stack trace if present.
PixlRestify checks to see if your Component contains a validate method. If it does the method is called for each request with the following arguments:
You validation method should return one of the following:
Alternatively, you can validate in your method handler and throw when validation fails.
FAQs
Create JSON/REST APIs with pixl-server framework
The npm package pixl-restify receives a total of 8 weekly downloads. As such, pixl-restify popularity was classified as not popular.
We found that pixl-restify 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
AI agents are pulling packages into environments no scanner is watching, creating exposure before security teams can see it.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.