পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Friday, February 17, 2017

ExpressJS - Serving static files

Static files are files that clients download as they are from the server. Create a new directory, public. Express, by default doesn't allow you to serve static files. You need to enable it using the following built-in middleware.

app.use(express.static('public'));
Note: Express looks up the files relative to the static directory, so the name of the static directory is not part of the URL.
Note that the root route is now set to your public dir, so all static files you load will be considering public as root. To test that this is working fine, add any image file in your new publlic dir and change its name to "testimage.jpg". In your views, create a new view and include this file like:
html
    head
    body
        h3 Testing static file serving:
        img(src="/testimage.jpg",alt="Testing Image")
You should get the output like below with your image:
Static files example

Multiple static dirs

We can also set multiple static assets directories using:
var express = require('express');
var app = express();

app.use(express.static('public'));
app.use(express.static('images'));

app.listen(3000);

Virtual path prefix

We can also provide a path prefix for serving static files. For example if you want to provide a path prefix like '/static', you need to include following code in your index.js file:
var express = require('express');
var app = express();

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

app.listen(3000);
Now whenever you need to include a file, for example, a script file called main.js residing in your public directory, use the following script tag:
<script src="/static/main.js" />
This technique can come in handy when providing multiple directories as static files. These prefixes can help distinguish between the multiple directories.

No comments:

Post a Comment