Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@methodus/server

Package Overview
Dependencies
Maintainers
3
Versions
158
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@methodus/server

dynamic rpc components

Source
npmnpm
Version
0.0.13
Version published
Weekly downloads
191
-38.78%
Maintainers
3
Weekly downloads
 
Created
Source

Methodus

Drawing Drawing

Methodus is a micro-service & RPC framework, so let's build a micro-service from scratch using this beautiful framework.

The controller

Methodus uses controllers in the same manner the express framework does. It binds functions or class methods to a route. and then routes the requests to these functions. in Methodus it will look like this

import { Method, MethodConfig, Files, Verbs, MethodType, Body, Response, Request, Param, Query, SecurityContext, MethodError, MethodResult } from '@tmla/methodus';

/*start custom*/
//in here you can put types and definitions that should be distributed with the contract
/*end custom*/

import * as fs from 'fs';
import * as path from 'path';



@MethodConfig('@tmla-tiles/hellow-world')
export class Hello {

@Method(Verbs.Post, '/api/hello', [upload.any(), autoReap]) //loading route and middlewares
public static async upload( @Files('0') file: any, @Query('originalname') originalname: string, @Query('keep_original') keepOriginalName: boolean = true) {
return new MethodResult(result);//return the result
};


@Method(Verbs.Get, '/api/hello/:file_id')
public static async getById( @Param('file_id') file_id) {
return new MethodResult(result);

};

@Method(Verbs.Get, '/api/hello/name/:file_name/')
public static async getByName( @Param('file_name') file_name, @SecurityContext() att) {
return new MethodResult(result);

};



@Method(Verbs.Delete, '/api/hello/id/:file_id')
public static async delete( @Param('file_id') file_id, @SecurityContext() att) {
return new MethodResult(deleteResult);
};
}

The controller class is decorated with a @MethodConfig decorator stating the name of the npm package the controller refers to. Each method is then decorated with a @Method decorator and the route parameters (Http verb, path, middlewares). The arguments passed into these decorated methods will be mapped according to the argument variables that precede them.

same as body in express, a name can be passed to get a specific key within the body object
DecoratorVerbDescription
@Body()Post
@Query()AllAll same as query in express, a name can be passed to get a specific key within the query object
@Param()AllAll same as param in express, a name can be passed to get a specific key within the param object.
@File()Postsame as files in express + multer , a name can be passed to get a specific key within the body object
@SecurityContext()Allsame as using req.att in our traditional cntrollers. the security context is built using the @tmla/secure middleware, therefor in order to use it the middleware should be used.
Special Mappings
@Response()Allsame as res in express, the mapping should be used when we need to pipe a stream to the response
@Request()All same as req in express, the mapping should be used when we need to pipe a stream to the request

A Methodus method should return an object of type MethodResult. this object can be used to set the status code for the response as well as paging and total records information.

@Method(Verbs.Get, '/api/hello/name/:file_name/')
public static async getByName( @Param('file_name') file_name, @SecurityContext() att) {
    return new MethodResult(result);
};

Methodus methods are automatiaclly loged using the Trace log level. you may have noticed the use of static methods for the controller class. this is not mandatory as you may use either static or instance approach, as long as you do that for all the methods in the class. Server activation The controller is ready, let's bind it to a methodus server. in our node app we create an entry point in the form of host.ts file. this host file starts an express server using the configured port and binds our controller to it.

import { ServerType, Server, MethodType, MethodusConfig } from '@tmla/methodus';
import { Hello } from './controllers/hello-controller';
const configuration = require('@tmla/config').config;
(async () => {
let config = new MethodusConfig();
config.run(ServerType.Express, { port: +configuration.port });
config.use(Hello, MethodType.Local, ServerType.Express);
let server = await new Server(+configuration.port).configure(config).start();
})()

the async function is an IIFE ( Immidiatly Invoked Function Expression) that executes the server code, but you may use any invocation method you see fit. if all goes well you should see

__ _ _|_|_ _ _| _ 
|||(/_ |_| |(_)(_||_|_> 
Starting REST server on port xxxx

which means that every thing wen well and you're ready to browser or postman your routes.

Your microservice ready, but if it is to be consumed using the Methodus rpc it need's to generate a contract.

FAQs

Package last updated on 26 May 2018

Did you know?

Socket

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.

Install

Related posts