How to Serve Static Content using Node.js ? Last Updated : 27 Apr, 2020 Comments Improve Suggest changes Like Article Like Report Accessing static files are very useful when you want to put your static content accessible to the server for usage. To serve static files such as images, CSS files, and JavaScript files, etc we use the built-in middleware in node.js i.e. express.static. Setting up static middleware: You need to create a folder and add a file. For example, app.js, To run this file you need to run the following command. node app.js Now create a folder whose content you want to serve as static, For example, you can create a folder named public. Now add some static content to this public folder. In this case, there is a GeeksLogo.png image in public folder. To serve this folder as static, you need the write this middleware in your index.js as follow: app.use(express.static(path.join(__dirname, 'public'))) where path is the global object and __dirname holds current directory address. Views is the folder where our all web pages will be kept. Now create a EJS file like Demo.ejs and put this file in views folder. Filename: Demo.ejs html <!DOCTYPE html> <html> <head> <title>Static Middleware Demo</title> </head> <body> <img src="/GeeksLogo.png" width="600" height="600" /> </body> </html> Filename: app.js javascript const express = require('express') const path = require('path') const app = express() // Static Middleware app.use(express.static(path.join(__dirname, 'public'))) // View Engine Setup app.set('views', path.join(__dirname, 'views')) app.set('view engine', 'ejs') app.get('/', function(req, res){ res.render('Demo') }) app.listen(8080, function(error){ if(error) throw error console.log("Server created Successfully") }) Steps to run the program: The project structure will look like this: Make sure you have 'view engine' like I have used "ejs" and also install express using the following commands: npm install ejs npm install express Run app.js file using below command: node app.js Open browser and type this URL: http://localhost:8080/ Then you will see the Demo.ejs page as shown below: So this is how you can serve static content to our server which is very helpful in serving images, CSS, js file, etc in your project. Comment More infoAdvertise with us Next Article How to Serve Static Content using Node.js ? G gouravhammad Follow Improve Article Tags : JavaScript Web Technologies Node.js Write From Home Node.js-Misc +1 More Similar Reads How to use Routes with serve-static Files in Node.js ? Using serve-static files with routes in a Node.js application involves creating a server with Express, defining routes, and serving static files like HTML, CSS, JavaScript, images, etc. Hereâs an article explaining how to use serve-static with routes in Node.js.Serving Static Files with Routes in No 3 min read How to do Templating using ExpressJS in Node.js ? Template Engine : A template engine basically helps us to use the static template files with minimal code. At runtime, the template engine replaces all the variables with actual values at the client-side. Templating Engine Examples: EJS (Embedded JavaScript Templating) Pug Mustache In this article w 2 min read How to Generate or Send JSON Data at the Server Side using Node.js ? In modern web development, JSON (JavaScript Object Notation) is the most commonly used format for data exchange between a server and a client. Node.js, with its powerful runtime and extensive ecosystem, provides robust tools for generating and sending JSON data on the server side. This guide will wa 3 min read How to build Video Streaming Application using Node.js ? In this article, we are going to create a Video Streaming Application. A video streaming application is used to stream or play video, like a simple video player.Functionality: Play videoApproach: We are going to use express for this project, and create a server file app.js then we will create an HTM 4 min read How to Send JSON Response using Node.js ? NodeJS is the runtime environment, which can execute the javascript code on any platform. It is widely used to create and run web application servers because of its salient features.During production, several times we need to send the resources or some type of information as a response, and javascri 5 min read Like