Koa2-TS-路由
安装相关依赖
npm install koa-router --save
npm isntall @types/koa-router --save-d
编写相关代码
新建 index.ts:
import Router = require("koa-router");
const router: Router = new Router();
router.get('/', (ctx: any) => {
ctx.body = 'router index';
});
router.get('/home', (ctx: any) => {
ctx.body = 'router home';
});
export default router;
新建 app.ts:
import Koa = require("koa");
import index from './routers/index';
const app = new Koa();
app.use(index.routes());
app.listen(3000, () => {
console.log('listen 3000 OK');
});
然后利用 ts 编译器把项目启动起来,然后在访问浏览器:http://127.0.0.1:3000/home


浙公网安备 33010602011771号