Koa
180 字
1 分钟
Koa
启动服务
const Koa = require("koa");const app = new Koa();app.use(async (ctx) => { ctx.body = "hello koa2";});app.listen(3000);console.log("koa 服务运行在 http://localhost:3000");
// 在控制台输入 node index.jskoa-router 路由中间件
const Koa = require('koa')const Router = require('koa-router')const app = new Koa()const router = new Router()router.get('/', async (ctx) => { ctx.body = 'hello koa2'})]router.get('/user', async (ctx) => { ctx.body = 'user'})app.use(router.routes())app.listen(3000)console.log('koa 服务运行在 http://localhost:3000')请求数据获取
const Koa = require('koa')const Router = require('koa-router')const app = new Koa()const router = new Router()router.get('/', async (ctx) => {
})router.get('/user', async (ctx) => { // 从上下文的request对象中获取 let request = ctx.request let req_query = request.query let req_querystring = request.querystring // 从上下文中直接获取 let ctx_query = ctx.query let ctx_querystring = ctx.querystring ctx.body = { url: ctx.url, req_query, req_querystring, ctx_query, ctx_querystring }}})支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
相关文章智能推荐
1
Python 笔记:工程实践与常见陷阱
学习笔记uv 包管理、导入系统、类型注解、面向对象、异常与常用标准库,写给前端开发者的工程速查。
2
Python 笔记:给前端开发者的语法对照
学习笔记用 JavaScript 的思维快速理解 Python 中真正不同的语法。
3
什么是 Agent
学习笔记希望可以帮到你
4
Vue3 封装命令式组件
学习笔记封装命令式组件的几种方式
5
Nuxt4备忘录
学习笔记希望可以帮到你
随机文章随机推荐






























































































































































