
Research
NPM targeted by malware campaign mimicking familiar library names
Socket uncovered npm malware campaign mimicking popular Node.js libraries and packages from other ecosystems; packages steal data and execute remote code.
rpc-controllers
Advanced tools
Use class-based controllers to create JSON-RPC 2.0 server usage in Express / Koa and TypeScript
Allows to create controller classes with methods as JSON-RPC methods that handle JSON-RPC 2.0 requests. You can use rpc-controllers with express.js or koa.js.
Install module:
npm install rpc-controllers --save
reflect-metadata
shim is required:
npm install reflect-metadata --save
and make sure to import it before you use rpc-controllers:
import "reflect-metadata";
Install framework:
a. If you want to use rpc-controllers with express.js, then install it and all required dependencies:
npm install express body-parser --save
Optionally you can also install their typings:
npm install @types/express @types/body-parser --save
b. If you want to use rpc-controllers with koa.js, then install it and all required dependencies:
npm install koa koa-router koa-bodyparser --save
Optionally you can also install their typings:
npm install @types/koa @types/koa-router @types/koa-bodyparser --save
Its important to set these options in tsconfig.json
file of your project:
{
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
Create a file MathController.ts
import {Controller, Method, Params} from "rpc-controllers";
@Controller("math")
export class MathController {
@Method("add")
add(@Params() params: Array<number>) {
return params.reduce((prev, curr) => prev + curr);
}
@Method("test")
test(@Params() params: any[]) {
return params;
}
}
This class will register JSON-PRC methods specified in method decorators in your server framework (express.js or koa).
Create a file app.ts
import "reflect-metadata"; // don't forget import this shim!
import {createExpressServer} from "rpc-controllers";
import {MathController} from "./MathController";
// creates express app, registers all controller methods and returns you express app instance
const app = createExpressServer({
controllers: [MathController] // we specify controllers we want to use
});
// run express application on port 3000
app.listen(3000);
if you are koa user you just need to use
createKoaServer
instead ofcreateExpressServer
Send a JSON-RPC 2.0 request to http://localhost:3000
using the method math.add
and the params [1, 1]
. You will get the result 2
.
import "reflect-metadata"; // don't forget import this shim!
import {createExpressServer} from "rpc-controllers";
const app = createExpressServer({
controllers: [__dirname + "/controllers/*.js"] // registers all given controllers
});
// run express application on port 3000
app.listen(3000);
If you have, or if you want to create and configure express app separately,
you can use useExpressServer
instead of createExpressServer
function:
import "reflect-metadata";
import {useExpressServer} from "rpc-controllers";
import {MathController} from "./MathController";
let express = require("express"); // or you can import it if you have installed typings
let app = express(); // your created express server
// app.use() // you can configure it the way you want
useExpressServer(app, { // register created express server in rpc-controllers
controllers: [MathController] // and configure it the way you need (controllers, validation, etc.)
});
app.listen(3000); // run your express server
rpc-controllers
supports a DI container out of the box.
You can inject your services into your controllers. Container must be setup during application bootstrap.
Here is example how to integrate rpc-controllers with typedi:
import "reflect-metadata";
import {createExpressServer, useContainer} from "rpc-controllers";
import {Container} from "typedi";
// its important to set container before any operation you do with rpc-controllers,
// including importing controllers
useContainer(Container);
// create and run express server
let app = createExpressServer(3000, {
controllers: [__dirname + "/controllers/*.js"],
});
That's it, now you can inject your services into your controllers:
@Controller()
export class MessageController {
constructor(private messageRepository: MessageRepository) {
}
// ... controller methods
}
FAQs
Use class-based controllers to create JSON-RPC 2.0 server usage in Express / Koa and TypeScript
The npm package rpc-controllers receives a total of 7 weekly downloads. As such, rpc-controllers popularity was classified as not popular.
We found that rpc-controllers 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.
Research
Socket uncovered npm malware campaign mimicking popular Node.js libraries and packages from other ecosystems; packages steal data and execute remote code.
Research
Socket's research uncovers three dangerous Go modules that contain obfuscated disk-wiping malware, threatening complete data loss.
Research
Socket uncovers malicious packages on PyPI using Gmail's SMTP protocol for command and control (C2) to exfiltrate data and execute commands.