Static file hosting
midway provides static resource hosting components based on the koa-static-cache module.
Related information:
Web support | |
---|---|
@midwayjs/koa | ✅ |
@midwayjs/faas | 💬 |
@midwayjs/web | ✅ |
@midwayjs/express | ❌ |
attention
💬 Some function computing platforms do not support streaming request responses. Please refer to the corresponding platform capabilities.
Installation dependency
$ npm i @midwayjs/static-file@3 --save
Or reinstall the following dependencies in package.json
.
{
"dependencies": {
"@midwayjs/static-file": "^3.0.0",
// ...
},
"devDependencies": {
// ...
}
}
Introducing components
First, introduce components and import them in configuration.ts
:
import { Configuration } from '@midwayjs/core';
import * as koa from '@midwayjs/koa';
import * as staticFile from '@midwayjs/static-file';
import { join } from 'path'
@Configuration({
imports: [
koa
staticFile
],
importConfigs: [
join(__dirname, 'config')
]
})
export class MainConfiguration {
}
Use
By default, the public
directory in the root directory of the project is hosted.
For example:
➜ my_midway_app tree
.
├── src
├── public
| ├── index.html
│ └── hello.js
│
├── test
├── package.json
└── tsconfig.json
You can directly use the path to access GET /public/index.html
and obtain the corresponding results.