1: 创建简单应用
安装:
npm install koa koa-router nodemon --save
app.js
const Koa = require('koa');
const Router = require('koa-router');
// 实例化koa
const app = new Koa();
const router = new Router();
// 路由
router.get('/', async ctx => {
ctx.body = { msg: 'Hello Koa Interfaces' };
});
// 配置路由
app.use(router.routes()).use(router.allowedMethods());
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`server started on ${port}!`);
});
执行:npm run nodemon