本文共 902 字,大约阅读时间需要 3 分钟。
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)
转载地址:http://hawiz.baihongyu.com/