简体   繁体   中英

Serving static files in Express (express.static vs app.static)

Through serving static files in Express.. I saw below code:

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?

Note: I tried to replace express with app and it said 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() has nothing to do with 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). Here's a description on the NPM wam.js page :

Wam is a small koa and next.js inspired middleware framework for node .

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()) . You can see in the Express doc for the app object , there is no mention of app.static() . That is apparently something that wasm.js invented for it's own framework.

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?

Because Express doesn't have app.static() . It has express.static() .

I also saw some NPM packages that use app.static() like wamjs for example, which is weird.

I wouldn't call it weird. wamjs is a different package with a different API. It is not Express so there should be no expectation that Express behaves like wamjs or that wamjs behaves like Express. They are different frameworks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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