,

Handling static files using Express JS in a Comprehensive NODE JS Tutorial

Posted by


In this tutorial, we will be discussing how to serve static files using Express JS, which is a popular web application framework for Node.js. Serving static files is a common requirement in web development, as it allows you to serve files such as images, CSS, JavaScript, and other static assets that are not dynamically generated by your server. This tutorial assumes that you have a basic understanding of Node.js and Express.

To get started, make sure you have Node.js installed on your machine. You can download it from the official Node.js website (https://nodejs.org/). Once you have Node.js installed, you can create a new directory for your project and navigate into it using the command line.

Next, you can initialize a new Node.js project by running the following command:

npm init -y

This will create a package.json file in your project directory, which will contain information about your project and its dependencies.

Now, you can install Express by running the following command:

npm install express

Next, create a new file called app.js in your project directory and add the following code to it:

const express = require('express');
const app = express();

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

app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000');
});

In this code, we are creating a new Express app and using the express.static() middleware to serve static files from the public directory. This means that any files in the public directory will be served to clients when they request them.

You can create a public directory in your project directory and add some static files to it, such as index.html, styles.css, and script.js.

public/
  index.html
  styles.css
  script.js

Now, you can run your server by running the following command:

node app.js

This will start your server on http://localhost:3000. You can access your static files by visiting http://localhost:3000/index.html, http://localhost:3000/styles.css, and http://localhost:3000/script.js in your web browser.

If you want to change the directory from which static files are served, you can pass a different directory as an argument to the express.static() middleware. For example:

app.use(express.static('assets'));

This will serve static files from the assets directory instead of the public directory.

In conclusion, serving static files in Express is a simple and straightforward process that can be done using the express.static() middleware. This allows you to serve static assets to clients and build web applications with static content. I hope this tutorial has been helpful in getting you started with serving static files in Express JS. Thank you for reading!

0 0 votes
Article Rating

Leave a Reply

18 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@inushapeiris
2 hours ago

Your teaching pace is excellent!

@mohamedibrahemsaad573
2 hours ago

I love you more than anything in this world

@eagle_facts5934
2 hours ago

Thank you for helping me to solve this problem❣

@User-sm4ld
2 hours ago

Sir your Nodejs course is far more better than paid courses.. THANK YOU SO MUCH❤

@chavamartinez3102
2 hours ago

Thank you for this, brother! You solved what chat-gpt couldn't.

@MuhammadUsman-ok5vs
2 hours ago

Awesome explanation

@poolman7786
2 hours ago

how do you use it with abp i am having problem with csp

@goryaudio
2 hours ago

Thank you very much

@AraizNaqvi
2 hours ago

Sir I really appreciate this video cause i spent almost 2.5 hours trying to fix this and i just couldn't. Thank you very much!

@mustafaahmed-v4p
2 hours ago

amazing

@littlewonder8641
2 hours ago

Awesome in all aspects.

@sunjin222
2 hours ago

Sir, just finished the series but you have not explained how to work with static files on the database

@ChaitanyaRepakula
2 hours ago

Please provide source code

@shanukaran1091
2 hours ago

How many more videos yet to come.

@sundayasikele9445
2 hours ago

Sir, Thanks a million for all the efforts you put in to provide us these free contents. My only appeal is for you to zoom into the tutorials enough for legibility. Not very clear to me, i have to strain my eyes to figure out the contents. You can equally make recording clear like that of Mosh. God bless you greatly

@rishiraj2548
2 hours ago

👍🙏🙏

@Aditya-gh4jy
2 hours ago

your node-js series videos are like TV Series episodes, they are addictive.

@solo2002
2 hours ago

Always waiting for your nodejs video sir…

18
0
Would love your thoughts, please comment.x
()
x