Koa2从零搭建restful API:
- 创建项目文件夹并进入
mkdir koa-project cd koa-project - 初始化项目
npm init - 安装 Koa
npm install koa koa-router --save - 编写示例代码,在
app.js文件中编写以下代码:// koa项目的入口文件 const Koa = require("koa"); //构造函数 const app = new Koa(); //声明一个实例 const port = 3000; //端口号 // 这里是所有的路由 const router = require("./router/list"); app.use(router.routes()).use(router.allowedMethods()) app.listen(port, () => { console.log(`server is running at http://localhost:${port}`); }); -
项目目录结构:

-
启动命令:
node app.js -
URL访问:
http://localhost:3000/apple
本文介绍了如何从头开始使用Koa2框架搭建RESTfulAPI,包括创建项目文件、配置Koa、安装依赖、设置路由器和监听端口,最后提供了一个简单的启动命令和访问URL示例。
1491

被折叠的 条评论
为什么被折叠?



