koa-router

const Koa = require("koa");
const Router = require("koa-router");
let app = new Koa();
let router = new Router();

router("/",async ctx=>{

    // 会将 / 和 /index的请求,返回同一页面
    ctx.redirect("/index");



    ctx.body = "主页";
});

router.get("/datail",async ctx=>{
    ctx.body = "详情页"
});

router.get("/getData",async ctx=>{
    // let indexData = fs.readFile("index.html");
    let indexData = "我是模拟数据";
    // ctx.body = {
    //     indexData
    // };

    ctx.render("/index2",{
        indexData
    });
});

// RESTful : 接口设计原则
// post / get

// 错误做法
// localhost:8000/adduser
// localhost:8000/deletuser
// localhost:8000/updateuser
// localhost:8000/getuser

// 正确做法
// localhost:8000/uesr  请求方式 get 获取
// localhost:8000/uesr  delete 删除
// localhost:8000/uesr  put 更新
// localhost:8000/uesr  post 添加

/*
    程序或者应用的事物都应该被抽象为资源
    每个资源对应唯一的URI(uri是统一资源标识符)
    使用统一接口对资源进行操作
    对资源
*/

app.use(router.routes());
app.listen(8888)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值