我使用 Express 为 Node.js 编写了以下非常简单的服务器:
'use strict';
const express = require('express');
const serveIndex = require('serve-index');
express()
.use(serveIndex(__dirname + '/public'))
.listen(1024);
虽然这确实可以很好地显示目录的内容,但当我单击列出的任何文件时,我会收到类似 Cannot GET/example.png
的错误消息。出了什么问题?
最佳答案
我打开了一个错误,他们最终修复了文档:
https://github.com/expressjs/serve-index/blob/master/README.md
tl;dr 版本:
var express = require('express')
var serveIndex = require('serve-index')
var app = express()
// Serve URLs like /ftp/thing as public/ftp/thing
// The express.static serves the file contents
// The serveIndex is this module serving the directory
app.use('/ftp', express.static('public/ftp'), serveIndex('public/ftp', {'icons': true}))
// Listen
app.listen(3000)
您必须先安装 express.static
中间件。
关于javascript - 无法访问 Express 中的 service-index 列出的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45808880/