Hello folks! Welcome back to a new edition of our tutorial on ExpressJS. In this tutorial guide, we are going to be discussing about how to serve static files in ExpressJS.
What are Static Files?
Static files are files that clients download as they are from the web server. Create a new directory, public. Express, by default doesn't permit you to serve static files. You need to enable it making use of the following built-in middleware -
app.use(express.static('public'));
Important - Express locates the files relative to the static directory, therefore 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 is going to be considered public as root. To test that this is working well, include any image file in your new public dir and change its name to "testimage.jpg". In your views, create a new view and add this file like this -
Note that the root route is now set to your public dir, so all static files you load is going to be considered public as root. To test that this is working well, include any image file in your new public dir and change its name to "testimage.jpg". In your views, create a new view and add this file like this -
html head body h3 Testing static file serving: img(src = "/testimage.jpg", alt = "Testing Image
Output
You should get the following output -
Multiple Static Directories
We can also set numerous static directories using the following code -
var express = require('express'); var app = express(); app.use(express.static('public')); app.use(express.static('images')); app.listen(3000);
READ: Express.js | Templating
Virtual Path Prefix
We can also make available a path prefix for serving static files. For example, if you want to provide a path prefix such as '/static', you need to include the following code below 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, utilize the following script tag -
<script src = "/static/main.js" />
This technique can be very effective when providing multiple directories as static files. These prefixes helps in discerning between multiple directories.
READ: Express.js | Middleware
Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we are going to be studying about ExpressJS Form Data.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.