美文网首页
koa-static处理静态资源

koa-static处理静态资源

作者: Wrestle_Mania | 来源:发表于2019-11-21 10:33 被阅读0次
const Koa = require("koa"),
  views = require("koa-views"),
  serve = require("koa-static"),
  router = require("koa-router")();

const app = new Koa();

// 可以指定多个静态目录
app.use(serve(__dirname + "/static"));
app.use(serve(__dirname + "/public"));

app.use(
  views("views", {
    map: {
      html: "ejs"
    }
  })
);

router.get("/", async ctx => {
  await ctx.render("index");
});

app.use(router.routes()).use(router.allowedMethods());

app.listen(8080);
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <link rel="stylesheet" href="style/index.css" />
    <link rel="stylesheet" href="style/common.css" />
  </head>
  <body>
    <p>yuweyuiw</p>
  </body>
</html>

相关文章

网友评论

      本文标题:koa-static处理静态资源

      本文链接:https://www.haomeiwen.com/subject/hguzictx.html