koa2-13

koa-static静态资源中间件

  在后台开发中不仅有需要代码处理的业务逻辑请求,也会有很多的静态资源请求。比如请求js,css,jpg,png这些静态资源请求。也非常的多,有些时候还会访问静态资源路径。用koa2自己些这些静态资源访问是完全可以的,但是代码会雍长一些。所以这节课我们利用koa-static中间件来实现静态资源的访问。

 安装koa-static:

npm install --save koa-static

 新建static文件夹

  然后在static文件中放入图片,css和js文件。

 使用koa-static中间件

  我们新建一个demo12.js文件,引入koa-static中间件,并用app.use方法进行使用。

const Koa = require('koa')
const path = require('path')
const static = require('koa-static')
 
const app = new Koa()
 
 
const staticPath = './static'
 
app.use(static(
  path.join( __dirname,  staticPath)
))
 
 
app.use( async ( ctx ) => {
  ctx.body = 'hello world'
})
 
app.listen(3000, () => {
  console.log('[demo] static-use-middleware is starting at port 3000')
})

猜你喜欢

转载自www.cnblogs.com/xiaofandegeng/p/9108913.html