How to introduce local files under Nodejs Express

Source: Internet
Author: User

The structure of Express is as follows:

|---node_modules------for installing local modules.

|---Public------------is used to store files, slices, script files, style sheets, etc. that users can download to.
|---Routes------------used to store routing files.
|---Views-------------templates for storing web pages.
|---the startup script for the App.js------------application.
|---The configuration file for the Package.json------project.
From the above structure, we know to put the local file into public, such as the script file JS file can be placed in the public folder JavaScripts. The answer to why it doesn't work in other places is as follows: The procedure for introducing a static file in App.js is as follows: App.use (Express.static (Path.join (__dirname, ' public '));//__ DirName is the absolute path when the program executes. In this way, the introduction of local files is indicated. So it is reasonable to put the local files into public, the following is the specific use in Express Ejs. For example, to introduce a local bootpicker.js file, simply add the code to the HTML head:
<Script src="/javascripts/datepicker.js" type="Text/javascript"></script>
when the browser issued a non-HTML file request, the server side to the public directory to find JavaScripts, and then to javascripts under the search for bootpicker.js files.

How to introduce local files under Nodejs Express

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.