用 koa-static 搭建静态资服务器没法显示文件列表吗?
 发布于 8 年前  作者 ouyangxuanyun  6713 次预览  最后一次回复是 8 年前  来自 问答 

用下面的代码简单的搭建一个静态资源服务,static目录包含111111.png

访问localhost:3000显示的是index.html内容,没有static目录的列表像这样这种: 222222.png

但是访问具体的文件都可以访问到,就是没办法显示整个列表,这样正常么?

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)
6 回复
atian25

因为这个中间件不支持生成目录

samcsf

要自己写,readDir撸一个很快的

ouyangxuanyun

@atian25 原来是这样 多谢多谢

ouyangxuanyun

@samcsf 恩恩 准备自己写一个了,还以为是自己用法不对

lvgithub

用中间件之前,最好先看源码后,再使用,这样可以避免很多问题

ouyangxuanyun

@lvgithub 恩是的,但是有时候着急使用就先用之后找时间再看源码了,多谢提醒!