简体   繁体   English

在 Express 中提供 static 文件(express.static 与 app.static)

[英]Serving static files in Express (express.static vs app.static)

Through serving static files in Express.. I saw below code:通过在 Express 中提供 static 文件。我看到以下代码:

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

// Initialize the main project folder
app.use(express.static('website'));

Why we didn't use app.static() instead of express.static() as we already assigned express() to the app constant, and what is the difference between them?为什么我们没有使用app.static()而不是express.static()因为我们已经将express()分配给了app常量,它们之间有什么区别?

Note: I tried to replace express with app and it said app.static is not a function .注意:我尝试用app替换express ,它说app.static is not a function I also saw some NPM packages that use app.static() like wamjs for example, which is weird.我还看到了一些使用app.static()wamjs包,例如 wamjs,这很奇怪。

app.static() has nothing to do with Express. app.static()与 Express 无关。

Wam is a completely different framework (that may be Express-like in some ways, but it's not Express and not identical to Express). Wam是一个完全不同的框架(在某些方面可能类似于 Express,但它不是 Express,也不等同于 Express)。 Here's a description on the NPM wam.js page :这是NPM wam.js 页面上的描述:

Wam is a small koa and next.js inspired middleware framework for node . Wam 是一个小型 koa 和 next.js 启发的节点中间件框架

If you want to program with Express, then use the Express documentation, not the Wam documentation and it will guide you to use app.use(somePath, express.static()) .如果您想使用 Express 进行编程,请使用 Express 文档,而不是 Wam 文档,它将指导您使用app.use(somePath, express.static()) You can see in the Express doc for the app object , there is no mention of app.static() .您可以在app object 的 Express 文档中看到,没有提及app.static() That is apparently something that wasm.js invented for it's own framework.这显然是 wasm.js 为它自己的框架发明的东西。

Why we didn't use app.static() instead of express.static() as we already assigned express() to the app constant, and what is the difference between them?为什么我们没有使用 app.static() 而不是 express.static() 因为我们已经将 express() 分配给了 app 常量,它们之间有什么区别?

Because Express doesn't have app.static() .因为 Express 没有app.static() It has express.static() .它有express.static()

I also saw some NPM packages that use app.static() like wamjs for example, which is weird.我还看到了一些使用 app.static() 的 NPM 包,例如 wamjs,这很奇怪。

I wouldn't call it weird.我不会说这很奇怪。 wamjs is a different package with a different API. wamjs 是不同的 package 和不同的 API。 It is not Express so there should be no expectation that Express behaves like wamjs or that wamjs behaves like Express.它不是 Express,因此不应期望 Express 的行为类似于 wamjs 或 wamjs 的行为类似于 Express。 They are different frameworks.它们是不同的框架。

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

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