Static Files For Pages

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 419:- Static Files For Pages

When setting up static files for pages in an Express.js application, you'll want to organize your static assets (such as CSS, JavaScript, images, and other resources) in a way that relates to the specific pages of your website. You can achieve this by creating dedicated folders for each page and placing the relevant static files inside those folders.

Let's go through the steps to set up static files for specific pages in an Express.js application:

Step 1: Create an Express Project Assuming you already have Node.js and npm installed, create a new directory for your project and initialize a new Node.js project.

bashCopy code

mkdir static-files-pages-demo cd static-files-pages-demo npm init -y

Step 2: Install Express Install Express.js using npm.

bashCopy code

npm install express

Step 3: Set Up an Express Server Create an app.js file in your project directory to set up an Express server.

bashCopy code

touch app.js

Edit the app.js file:

javascriptCopy code

// app.js const express = require('express'); const app = express(); const path = require('path'); // Serve static files for the home page app.use('/home', express.static(path.join(__dirname, 'public', 'home'))); // Serve static files for the about page app.use('/about', express.static(path.join(__dirname, 'public', 'about'))); const port = 3000; app.listen(port, () => {  console.log(`Server is running on http://localhost:${port}`); });

Step 4: Create a 'public' Folder Create a folder named public in your project directory to store your static files for different pages.

bashCopy code

mkdir public

Step 5: Organize Static Files Inside the public folder, create subfolders for each page (e.g., home and about). Place the relevant static files for each page in their respective folders.

arduinoCopy code

static-files-pages-demo/  |- app.js  |- public/    |- home/       |- css/          |- home-styles.css       |- js/          |- home-scripts.js    |- about/       |- css/          |- about-styles.css       |- js/          |- about-scripts.js

Step 6: Access Static Files for Each Page Now, you can access the static files for each page in your Express application. For example, when rendering the home page, you'll load the CSS and JavaScript files specific to that page.

In your index.ejs file for the home page:

htmlCopy code

<!-- Inside your home page (index.ejs) --> <!DOCTYPE html> <html> <head>  <title>Home Page</title>  <link rel="stylesheet" href="/home/css/home-styles.css"> </head> <body>  <!-- Your HTML content here -->  <script src="/home/js/home-scripts.js"></script> </body> </html>

In your about.ejs file for the about page:

htmlCopy code

<!-- Inside your about page (about.ejs) --> <!DOCTYPE html> <html> <head>  <title>About Page</title>  <link rel="stylesheet" href="/about/css/about-styles.css"> </head> <body>  <!-- Your HTML content here -->  <script src="/about/js/about-scripts.js"></script> </body> </html>

Step 7: Start the Server Run the following command to start the Express server:

bashCopy code

node app.js

Step 8: Test Static File Access for Pages Open your web browser and visit http://localhost:3000/home for the home page and http://localhost:3000/about for the about page. The CSS and JavaScript files specific to each page should be loaded and applied accordingly.

Congratulations! You have successfully set up static files for specific pages in your Express.js application. This approach allows you to organize your static assets in a structured manner, making it easy to manage and serve the relevant files for each page of your website.

49. Beginning The Major Project - 2

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?