Express Details inquiry (1)--app.use (express.static)

Source: Internet
Author: User

Express believes that many people use Nodejs to build a server's preferred framework, a lot of tutorials, but also teach you how to use. If you want to know more about his details, you might as well look into it with me.

First look at a method that everyone uses App.use (express.static (__dirname+ '/public '));

You must have used it this way in your app file:

  App.use (express.static (__dirname + '/public ')); // set the static file directory

As the note says, this means that the static file directory is set to the project root +/public, which you might also write:

App.use (Express.static (Path.join (__dirname, ' public ')); // It's the same as the top.

The join method using the Path module is equivalent to the previous one, note that the path module is introduced here.

Have you ever wondered why this sentence can help us set up a static file directory? What the hell did he do?

In order to take care of beginners, we might as well start with the most basic static resource file, what is a static resource file?

Static resource files are popular and can be understood as a file that does not change content for different users. For example, whether it is Dick and Harry or Harry visit Baidu, they receive the picture, CSS files and front-end JavaScript files are the same, we call such files as static resource files.

So the response, for different users to make different response is dynamic file, Dick and Harry Harry login Baidu, Baidu will show them "Hello Zhang San", "Hello John Doe", "Hello Harry", then responsible for such dynamic logic file is dynamic file, according to you are using the technology is different, The dynamic file may be a. jsp file, a php file, or a server-side JS file for our node. js.

So what does the App.use () method do? Let's take a look at what the official API says:

The main idea is that App.use is used to register the middle function for path, the path by default is '/', that is, to handle any request, but also note that he will handle the path under path, such as if the path is set to '/hello ', then when the request route is '/hello/', Requests such as '/hello/nihao ', '/hello/bye ' will be given to the intermediate function for processing.

So we now know that App.use (express.static (__dirname + '/public ')) is to handle all requests, first to express.static (__dirname + '/public '), Although we do not know the processing details of express.static (), it does not affect our speculation, at least we can know that the return value of Express.static () is definitely a function.

The official documentation is very detailed, as we explained some of the details of Express.static (), which I simply translated to you:

Serving static files in Express

To provide services for static resource files (Pictures, csss files, JavaScript files), use the intermediate functions built into express express.static .

Pass a directory containing static resources to the express.static middleware to start providing the files immediately. For example, use the following code to provide images, CSS files, and JavaScript files in the public directory:

App.use (express.static (' public '));

Now, you can loadpublic目录下的文件了:

http://localhost:3000/images/kitten.jpghttp://localhost:3000/css/style.css http://localhost:3000/js/app.jshttp://localhost:3000/images/bg.pnghttp: // localhost:3000/hello.html

Express will look for files in a static resource directory, so you do not need to use a static directory as part of the URL.

express.staticadd multiple static resource directories by using middleware multiple times:

App.use (express.static (' public ')); App.use (express.static (' files '));

Express will look for the static resource file in the order you set the static resource directory.

In order to create a virtual file prefix for a static resource file (which does not actually exist in the file system), you can use the express.static function to specify a virtual static directory, as follows:

App.use ('/static ', express.static (' public '));

Now you can use it /static as a prefix to load the public files under the folder.

http://localhost:3000/static/images/kitten.jpghttp://localhost:3000/static/css/ Style.csshttp://localhost:3000/static/js/app.jshttp://localhost:3000/ Static/images/bg.pnghttp://localhost:3000/static/hello.html

However, the path you provide to express.static the function is a relative path relative to the start location of the node process. If you start the Express app in another folder, the more secure way is to use the absolute path of the static resource folder:

App.use ('/static ', express.static (__dirname + '/public '));

Express Details inquiry (1)--app.use (express.static)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.