koa --- 跨域,解析POST参数、路由配置
生活随笔
收集整理的這篇文章主要介紹了
koa --- 跨域,解析POST参数、路由配置
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目標(biāo)
將開發(fā)中經(jīng)常遇見的問(wèn)題寫在這里方便查詢.
使用Koa創(chuàng)建一個(gè)簡(jiǎn)單的服務(wù)器
const Koa = require("koa"); const app = new Koa(); app.listen(3000, () =>{console.log("[server] Server is running at http://localhost:3000") })使用koa2-cors解決跨域問(wèn)題
const cors = require("koa2-cors"); app.use(cors());使用koa-bodyparser解決POST請(qǐng)求參數(shù)解析問(wèn)題
// 配置POST請(qǐng)求解析 const bodyParser = require("koa-bodyparser"); app.use(bodyParser()); // 配置路由 const Router = require("koa-router"); const router = new Router({prefix, "/users"});// 使用 router.post("/", async ctx=>{// 上面配置了bodyParser所以可以直接解析console.log(ctx.query);ctx.body = {ok:1} })app.use(router.routes());總結(jié)
以上是生活随笔為你收集整理的koa --- 跨域,解析POST参数、路由配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python日志输出 超量 停止_lin
- 下一篇: HTML5 响应式图片