1.安装模块
npm install koa koa-router --save npm install -g nodemon
2.入口文件
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}`) })
3.package.json
{ "name": "koa_app", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "start": "node app.js", "nodemon": "nodemon app.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "koa": "^2.7.0", "koa-router": "^7.4.0" } }
.