Recently, I have nothing to do with my own business, and I have been practicing my skills. So it sprouts. Can I write a nodejs springmvc with the decorator of typescript, and then I have this project.
The project supports:
Dependency injection controller, service
Inject get / post / put / delete / patch and other rest methods
Parse parameters of rest API, such as requestparam
Upload file supports multer
Support direct debug typescript code in vscode
Students who want to learn how to debug typescript code can pay attention to it. It’s really useful.
Some contents of readme directly:
Installation
npm i easy-node-ioc --save
Quick Start
Check out the quick start example in test
.
Usage
1.Create a Controller
import { Controller} from 'easy-node-ioc';
@Controller('/test')
class TestControl {
...
}
2.Create a Service
import { Service } from 'easy-node-ioc';
@Service('')
class TestService {
...
}
3.Inject Service
import { Autowired,Controller } from 'easy-node-ioc';
@Controller('/test')
class TestControl {
@Autowired
testService: TestService;
...
}
4.Define Rest API:GET,POST,PUT,DELETE,PATCH
import { Autowired,Controller,GET,RequestParam } from 'easy-node-ioc';
@Controller('/test')
class TestControl {
@Autowired
testService: TestService;
@Get('/index')
index(@RequestParam('age') age: number, req: Request, res: Response) {
console.log('index method');
this.dbService.queryDb();
res.status(200).send(this.testService.queryDb());
}
...
}
4.Define Start App
import { Bootstrap, ComponentScan } from "../";
@ComponentScan(join(__dirname, "./Controller.ts"))
@Bootstrap
class App {
constructor() {}
app = express();
main() {
const server = http.createServer(this.app);
server.listen(9001, function() {
console.log("Example app listening at http://%s:%s");
});
}
}
How to debug
if you use vscode , just follow .vscode/launch.json
, select Launch Program
.
if you see Example app has started.
in the console , then means test case start successfully .
Try to call http://localhost:9001/api/test/index
.
GitHub address: https://github.com/chenkang08
Note: because this project is also a whim, there may be problems. However, at present, I have used this package to rewrite a node background project in my company. At present, everything is running well.
At the same time, welcome to issues. If you think it’s OK, you can also give me a star.