中间件koa-static

  在后台开发中往往有很多的静态资源请求,如请求js,css,jpg,png这些静态资源,接下来利用koa-static中间件来实现静态资源的访问。

【安装koa-static】

 npm install koa-static --save --registry=https://registry.npm.taobao.org 

【建立static文件夹】
  新建static文件夹,然后在static文件夹中放入图片,css和js文件。

【引用static中间件】

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('server is starting at port 3000')
})

现在静态资源已经部署到服务器上了,静态资源的路径在server.js中已经配置好了,只需要输入静态资源文件名即可。


15401334-71fb4b5eca9cea3b.png

15401334-950fd517caad771b.png

猜你喜欢

转载自blog.csdn.net/weixin_33943836/article/details/88163134