简体   繁体   English

如何使用快递服务目录?

[英]how to serve a directory with express?

I would like to create a simple express server that sends a directory like the image following: Browser directory picture我想创建一个简单的快递服务器,它发送的目录如下图:浏览器目录图片

const express = require('express');
const path = require('path');
const app = express();

app.use(express.static(path.join(__dirname, 'shaders')));

app.use('*', (req, res) => {
  res.sendFile((path.join(__dirname, 'shaders')));
});

const PORT = 3000;
app.listen(PORT, () => {
  console.log('listening on port ', PORT);
});

This code displays Cannot GET / in the browser window.此代码在浏览器窗口中显示Cannot GET /

您可以使用静态文件夹通过 GET 请求共享或获取文件。

app.use(express.static(path.join(__dirname, 'shaders')));

This code displays Cannot GET / in the browser window.此代码在浏览器窗口中显示无法获取 /。

Sending a GET to / will fallback to your app.use * as you don't have a route defined.发送 GET 到 / 将回app.use您的app.use * 因为您没有定义路由。 It's not clear what this should do as you're returning a directory instead of a file, which isn't going to work.目前尚不清楚这应该做什么,因为您返回的是目录而不是文件,这是行不通的。

If you'd like to access a specific file, you need to request it directly as localhost:3000/shaders/xxx , etc. The use of express.static appears to be correct.如果你想访问一个特定的文件,你需要直接请求它作为localhost:3000/shaders/xxx等。 express.static的使用似乎是正确的。

Using a library使用库

There are libraries that already do this for you, for example serve-index .有一些库已经为你做了这件事,例如serve-index

Doing it yourself自己做

This is a modified version of your code to show file content or list the files/directories in a directory.这是代码的修改版本,用于显示文件内容或列出目录中的文件/目录。 I've added some comments to explain what's happening, but feel free to ask more questions if something is not clear.我已经添加了一些评论来解释正在发生的事情,但如果有什么不清楚的地方,请随时提出更多问题。

const express = require("express");
const path = require("path");
const fs = require("fs");
const app = express();

const listingPath = path.join(__dirname, "shaders");

app.get("*", (req, res) => {
    // Build the path of the file using the URL pathname of the request.
    const filePath = path.join(listingPath, req.path);

    // If the path does not exist, return a 404.
    if (!fs.existsSync(filePath)) {
        return res.status(404).end();
    }

    // Check if the existing item is a directory or a file.
    if (fs.statSync(filePath).isDirectory()) {
        const filesInDir = fs.readdirSync(filePath);
        // If the item is a directory: show all the items inside that directory.
        return res.send(filesInDir);
    } else {
        const fileContent = fs.readFileSync(filePath, 'utf8');
        // If the item is a file: show the content of that file.
        return res.send(fileContent);
    }
});

const PORT = 3000;
app.listen(PORT, () => {
    console.log("listening on port ", PORT);
});

You can use this as a base to make a template that includes links to the files/directories, to include a link to the parent directory, to show more meta data ...您可以使用它作为基础来制作包含文件/目录链接的模板,包含指向父目录的链接,显示更多元数据......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM