> MKOA整合了静态文件服务功能。
使用static前安装相关依赖:
~~~
npm install koa-send --save
npm install koa-static --save
~~~
修改config.js文件,开启静态资源中间件。(更多配置项查看具体MKOA文档,生产环境配合nginx可以关闭该功能)
~~~
static_open:true
~~~
> 创建公共静态资源文件夹
在项目目录新建static文件夹。
在static文件夹创建index.html文件
~~~
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
HELLO WORLD!
</body>
</html>
~~~
重启项目,访问http://localhost:3000/index.html 或者 http://localhost:3000/
现在,静态文件服务已经正常启动。
> 模块目录支持独立的静态文件服务,我们在上一章建立的test模块下,建立static文件夹。复制index.html文件到里面。
此时目录结构如下:
~~~
-application
- - test
- - - controller
- - - - index.js
- - - static
- - - - index.html
- config
- - development
- - - config.js
- - config.js
- static
- - index.html
- app.js
- package.json
~~~
访问http://localhost:3000/test/static/index.html 正常返回test模块下static/index.html内容。