Coder Social home page Coder Social logo

minmin's Introduction

Apache V2 License

1. Introduction

MinMin is a tiny web framework entirely written in typescript, based on ExpressJS and inspired by Java Web

2. How to use

Getting started

Install minmin

npm install --save minmin

Change tsconfig.json looks like

{
  "compilerOptions": {     
      "lib": [
      "dom",
      "es2015"
      ],
      "target": "es5",
      "moduleResolution": "node",
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,      
  }
}

Define controller

Firstly we create ApiController.ts file then define controller with base url is /api

@Controller('api')
class ApiController {
}

Define request handler

The next step, we need define request handler like this

@Controller('api')
class ApiController {

  @Post('login')
  private async login(@Data('username') username: string,
                      @Data('password') password: string) {
    let user = await User.findOne({username: username});
    if (user) {
      let compare = await user.comparePassword(password);
      if (compare) {              
        return new Result('user', user);
      } else {
        return new Error(401, "Invalid username or password.");
      }
    } else {
        return new Error(404, "Username not found.");
    }
  }
}

The upper code is equivalent to http method handler in expressjs like bellow

app.post('/api/login', function(req, res) {
   var username = req.body.username
   var password = req.body.password  
   ...
})

Start server

The last step is starting web server

import {WebServer} from "minmin"
import './controllers/ApiController' // import the controller here (very important)

const server = new WebServer();
server.setPort(3000);
server.start();

Support Dependency Injection

Support dependency injection since version 0.0.32

import {Controller, Service, Inject} from "minmin"

@Service()
class MyService {

  action(): void {
  }
  
}

@Controller('api')
class ApiController {

  @Inject()
  myService: MyService;
  
  
}

3. Decorators list

Methods

@Get @Post @Put @Delete

Parameters

@Param @Query @Data @Session (deprecated) @Request

Dependency Injection

@Inject @Service

4. Classes list

WebServer Result Error View

5. Template and demo

minmin's People

Contributors

minhdtb avatar minhdtb83 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.