创建package.json
{
"name": "decorator-demo","version": "1.0.0","main": "index.js","license": "MIT","dependencies": {
"@tuzilow/express-decorator": "^0.0.3","express": "^4.17.1"
},"devDependencies": {
"@babel/cli": "^7.11.6","@babel/core": "^7.0.0","@babel/node": "^7.0.0","@babel/plugin-proposal-class-properties": "^7.10.4","@babel/plugin-proposal-decorators": "^7.10.5","@babel/preset-env": "^7.0.0","babel-eslint": "^9.0.0"
},"scripts": {
"start": "babel-node index"
}
}
创建.babelrc
{
"plugins": [
["@babel/plugin-proposal-decorators",{ "legacy": true }],["@babel/plugin-proposal-class-properties",{ "loose": true }]
],"presets": ["@babel/preset-env"]
}
创建index.js并编写代码
import express from 'express';
const Server = express();
Server.get('/',(req,res) => {
res.json({
title: 'hello world',});
});
Server.listen(3000,() => {
console.info('running in http://localhost:3000');
});
import express from 'express';
import { Controller,Get } from '@tuzilow/express-decorator';
const Server = express();
@Controller
class User {
@Get('/')
home(req,res) {
res.json({
title: 'hello world',});
}
}
Server.use(new User());
Server.listen(3000,() => {
console.info('running in http://localhost:3000');
});
import express from 'express';
import { Controller,Get,RootUrl } from '@tuzilow/express-decorator';
const Server = express();
@Controller
class User {
@RootUrl('/user') url() {}
@Get('/')
home(req,});
}
@Get('/list')
list(req,res) {
res.json({
title: 'this is a list',() => {
console.info('running in http://localhost:3000');
import express from 'express';
import { Controller,RootUrl,Post } from '@tuzilow/express-decorator';
const Server = express();
@Controller
class User {
@RootUrl('/user') url() {}
@Get('/')
home(req,});
}
// query传参
@Get('/getOne')
getOne(req,res) {
const { id } = req.query;
res.json({
title: 'hello world',id,});
}
// params传参
@Get('/list/:id')
getItem(req,res) {
const { id } = req.params;
res.json({
title: 'hello world',});
}
// body传参
@Post('/create')
create(req,res) {
const { id } = req.body;
res.json({
code: 0,() => {
console.info('running in http://localhost:3000');
});