mvc-ts is a mvc framework bult from express module.
Installation
npm install mvc-ts --save
Usage
ENV
mvc-ts will read .env file to load custom env.
# Example .env file PORT=3333
application
; @ {} const application = ; applicationstart;
application will start a server on port 3333, and make folders for config, db, log, public.
load model
// Example oauth2@3.x;; application;
Middleware
// middleware example for oauth// file OauthAuthenticateMiddleware.ts; ;; @ @ private application: ApplicationLoader; public async const request = req; const response = res; const token = await thisapplication; requser = tokenuser; ;
Model
file HellWorldModle.ts
; @ say: string = 'world';
Service
file HelloService.ts
;; @ @ private hello: HelloModel; public { return `hello `; }
Rest api Controller
example get api controller
; ; ; @ @ private helloService: HelloService; @ public async let word = thishelloService; res;
now open url http://127.0.0.1:3333 in browser, it will show hello world in the page.
Config
define you any config in file __dirname/config/*.json
and you can get any config value by ConfigContainer
file __dirname/config/utils.json
file __dirname/config/vendor.json
file my.ts
; console;// log: mvcconsole;// log: authorization_codeconsole;// log: 7b04928caf221bf56e*********
Mongodb
configure self mongodb config in file __dirname/config/database.json
__dirname/config/database.json
UserModel.ts
;; @@ _id: MongodbObjectID; name: string; age: number; birthday: Date;
UserService.ts
;; ; @ @ private user: UserModel; public async : Promise<string> let user = await thisuser; return username public async : Promise<number> let db = MongoContainer; let user = await dbuser; return userage;
UserController.ts
;;; ; @ @ private userService: UserService; @ public async let _id = Mongodb; let age = await thisuserService; res; @ public async let _id = Mongodb; let name = await thisuserService; res;
now, you can get user name by url http://127.0.0.1:3333/user/:uid/name; now, you can get user age by url http://127.0.0.1:3333/user/:uid/age;
Redis
RedisService.ts
; @@ private client: Reidsclient; public async { return await thisclient; } public async { return await thisclient; }