前言
大家好 我是歌谣 今天给大家带来node中关于koa静态资源模块的讲解
初始化项目
npm init -y
安装koa和路由
npm i koa
npm i koa-router
npm i koa-static
配置文件
{
"name": "koa",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"koa": "^2.14.2",
"koa-router": "^12.0.1",
"koa-static": "^5.0.0"
}
}
indexRouter.js
const Koa = require("koa")
const static = require("koa-static")
const path=require("path")
const app = new Koa()
const router=require("./routes/index")
app.use(static(path.join(__dirname,"public")))
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
歌谣爱你
</div>
</body>
</html>