Open static resource files (detailed introduction)

What are static resources

Like js, css, image these are static resources.

Why open up static resources

When developing a website, I am used to putting static resources and the original html file (template file) in two different folders. If you do not open the static resource template file, you cannot access the static resource and cannot generate a normal web page. The so-called open static resources is to splice the user's requested page with the actual storage path of the file, so that the page accessed by the user can load the css, js files normally

app.use(express.static(path.join(__dirname,'静态资源路径名')));

The absolute path of the file introduced in the template should start with / to be regarded as an absolute path

Steps to open static resource files

  1. Import expess framework and path module
  2. Intercept request
  3. Create a web server
  4. Put all static resources in a folder
  5. Open static resource files
const app = express();

app.use(express.static(path.join(__dirname, 'public')))

Then you can use the public folder as the directory of static files.
When you need to import static files, you must import them with absolute paths. For
example:

<link rel="stylesheet" href="/admin/css/base.css">

Self-motivation

Everyone will be tired, no one can bear all the sorrows for you, and there will always be some time for people to learn to grow up by themselves.

Guess you like

Origin blog.csdn.net/weixin_50001396/article/details/112641361