Typescript
An easy http restful framework written by
Package includes both compiled javascript
and typescript
declaration(.d.ts) files
Developers can continue coding in both typescript
and javascript
Features:
- Basic routes
- Group routes
- Middleware
- Custom exception handler
- Log supports
- NOT support optional route path like
/path/[maybe]
(The author doesn't want to do that. (╯‵□′)╯ ┻━┻)
Installation:
npm install --save ts-echo
Usage:
Basic routes:
; // create router; // define routesrouter.GET"/hello", ; // run serverServer.loadRouterrouter;Server.run3000, "127.0.0.1";
Access parameters:
// create router; // define routesrouter.PATCH"/user/{user_id}", ;
Response:
// create router; // define routesrouter.GET"/string", ;router.GET"/json", ;router.GET"/json/but/string", ;
Group routes:
// create router; // define routesrouter.group"/v1",;
Middleware:
// create router; // define routes with middlewarerouter.group"/v1",, ;// the order of middleware execution should be:// before group -> before single -> after single -> after group Server.loadRouterrouter;// ...
Log:
// enable log printServer.printLogstrue; // then you can use the default logger which will print logs to console// or set your logger which must be a winston.Logger;Server.setLoggerlogger; // set logger before you load router // load router as alwaysServer.loadRouterrouter; // run server and execute the callback functionServer.run3000, "127.0.0.1",;
Exception handler:
// set custom exception handler to catch and handle errorsrouter.setExceptionHandler;router.GET"/not-found", ;// then load routerServer.loadRouterrouter;// ...
javascript
:
All together in const Server = Server;const Router = Router;const HttpException = HttpException;const Middleware = Middleware; // create a routerconst router = ; // add routesrouter; // set custom exception handler to catch and handle errorsrouter;router; // access request body, query params and route paramsrouter // with middlewares async { console; } async { console; } async { console; } async { console; }const em = ;const am = ;router; // start the server, load routes and run// enable log printsServer; // set custom logger// logger must be a winston.LoggerInstanceconst winston = ;const logger = level: "debug" transports: label: "APP_NAME" align: true colorize: true timestamp: true ;Server;Server;Server;