MinMin is a tiny web framework entirely written in typescript, based on ExpressJS and inspired by Java Web
npm install --save minmin
Change tsconfig.json looks like
{
"compilerOptions": {
"lib": [
"dom",
"es2015"
],
"target": "es5",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
}
}
Firstly we create ApiController.ts
file then define controller with base url
is /api
@Controller('api')
class ApiController {
}
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
...
})
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 since version 0.0.32
import {Controller, Service, Inject} from "minmin"
@Service()
class MyService {
action(): void {
}
}
@Controller('api')
class ApiController {
@Inject()
myService: MyService;
}
@Get
@Post
@Put
@Delete
@Param
@Query
@Data
@Session
(deprecated)
@Request
@Inject
@Service
WebServer
Result
Error
View
Redirect