Home > Web Front-end > CSS Tutorial > How to Properly Include CSS Files in Node.js, Express, and EJS Projects?

How to Properly Include CSS Files in Node.js, Express, and EJS Projects?

Patricia Arquette
Release: 2024-10-26 00:40:28
Original
913 people have browsed it

How to Properly Include CSS Files in Node.js, Express, and EJS Projects?

Including CSS Files in Node, Express, and EJS Applications

When working with Node.js, Express, and EJS, you may encounter difficulties including CSS files in your project. This issue arises when the CSS file's type is incorrectly set as 'text/html' instead of 'text/css' in the developer's console.

To resolve this issue, follow these steps:

In the server.js file:

<code class="js">app.use(express.static(__dirname + '/public'));</code>
Copy after login

This line ensures that static files, including CSS, are served from the 'public' directory.

In the EJS file:

<code class="ejs"><link rel="stylesheet" type="text/css" href="css/style.css"></code>
Copy after login

Notice that you do not need to include a leading slash '/' before the 'css' directory.

Explanation:

  • Including the '/public' directory as the static files path ensures that CSS files are served correctly.
  • Specifying the correct CSS file path in EJS prevents errors in loading the stylesheet.

The above is the detailed content of How to Properly Include CSS Files in Node.js, Express, and EJS Projects?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template