Use Typescript's decorator implement auto injection just like Spring MVC, the web framework is Express.
npm i easy-node-ioc --save-dev
git clone https://github.com/chenkang084/easy-node-ioc.git
npm i
NODE_ENV=development npx ts-node demo/App.ts
When you execuate above commands , the Example app has started
will display in your termial , then means web application started and worked well, if you try to input http://localhost:9001/api/test/index in your browser , you will see OK in your browser.
import { Controller} from 'easy-node-ioc';
@Controller('/test')
class TestControl {
...
}
import { Service } from 'easy-node-ioc';
@Service('')
class TestService {
...
}
import { Autowired,Controller } from 'easy-node-ioc';
@Controller('/test')
class TestControl {
@Autowired
testService: TestService;
...
}
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());
}
...
}
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');
});
}
}
if you use vscode , just follow .vscode/launch.json
, select Launch Test Case
.
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
.
add create-easy-node-ioc project client support swagger-ui