一、 启动一个node.js的项目

1-1 文件初始为npm仓库

  1. cnpm init -y
  2. # 生成package.json文件

1-2 安装依赖

cnpm i koa koa-router -S
#下载完毕后会生成node_modules

1-3 实现hello world

/* 1、导入koa这个模块 */
const koa = require("koa");
/* 2、新建一个应用 */
const app = new koa();
/* 3、给前端返回值 */
app.use(async ctx=>{
    ctx.body = "hello world"
})
/* 4、app监听8080 */
app.listen(8080);

1-4 启动服务

nodemon index.js

1-5 在浏览器中查看

http://localhost:8080/

二、 路由

/my  my.html
/friend friend.html


/pages/index/index     index.wxml
/pages/music/music     music.wxml

1-1 配置

const koa = require("koa");
const app = new koa();

/* 1、导入路由 */
const router = require("koa-router")();

/* 2、设置路由 */
router.get("/my",async ctx=>{
    ctx.body = {
        code:200,
        msg:"my"
    }
})
/* 3、配置路由 */
app.use(router.routes())



app.listen(8080);
/top/playlist 
1、默认 5条数据
{
    code:200,
    data:[5],
    total:25
}
/top/playlist?offset=2&limit=2

三、后台获取前端的get串值

http://localhost:8080/top/playlist?offset=2&limit=2


router.get("/top/playlist",async ctx=>{
    console.log(ctx.request.query)
    ...
})