简体   繁体   English

Express 中的 express.static 是什么?

[英]What is express.static in Express?

I am new in Node/Express .我是Node/Express新手。 I am trying to build a static website using Express.我正在尝试使用 Express 构建一个静态网站。 I have an assets directory and some pages on the root directory of the project.我在项目的根目录上有一个资产目录和一些页面。 By googling, got some resources there I got a statement like this:通过谷歌搜索,在那里获得了一些资源,我得到了这样的声明:

app.use('/assets', express.static(__dirname + '/assets'));

I know about __dirname is a current working directory and app.use() acts as a middleware function, unlike app.get() and so on.我知道__dirname是当前工作目录,而app.use()充当中间件函数,与app.get()等不同。 By searching about express.static got documentation link Serving static files in Express通过搜索express.static得到文档链接Serving static files in Express

But I am unclear and confused.但我不清楚和困惑。 I hope someone will be able to help me and thanks in advance.我希望有人能够帮助我并提前致谢。

express.static exposes a directory or a file to a particular URL so it's contents can be publicly accessed. express.static向特定 URL 公开目录或文件,以便可以公开访问其内容。

From your example:从你的例子:

app.use('/assets', express.static(__dirname + '/assets'));

Assuming the /assets directory contains 2 images, foo.jpg and bar.jpg then you can simply access them at:假设/assets目录包含 2 个图像, foo.jpgbar.jpg那么您可以简单地访问它们:

There's nothing more to it.没有更多了。

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

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