11-26 怎样和数据库打交道

// 引入对象
const Koa = require('koa');
const koaBody = require('koa-body');
const Router = require('koa-router')
//引入koa-router
// 引入内部方法或属性
// const({方法或属性名})= reqire('koa');
//创建对象
const app = new Koa();
app.use(koaBody());
const  router = new Router();//创建路由支持传递参数
get
// app.use(async (ctx,next) => {
//   ctx.body = 'Hello Koa2';
//   next();
// });
router.get("/", async ctx=>{
  //url参数  使用query
  // 请求地址 url 网络路径后面的所有东西  http://localhost:3000后面的所有东西
  // console.log(ctx.URL);
  console.log(ctx.query);
  // console.log(ctx.querystring);
})
//根目录 返回地址(ctx 随便写 形参)
// ctx现在就是获取到的东西
app.use(router.routes());//使用路由对象里的所有路由 上面配置路由
app.use(router.allowedMethods({
    //这里面所有支持的方法 可以为空
    // throw: true, // 抛出错误,代替设置响应头状态
    // notImplemented: () => '不支持当前请求所需要的功能',
    // methodNotAllowed: () => '不支持的请求方式'
}));




//监听  locallhost:3000 端口号  
app.listen(3000,()=>{
  console.log("http://localhost:3000")
});
posted @ 2021-11-26 12:16  林启  阅读(42)  评论(0)    收藏  举报