0
0
Expressframework~15 mins

express.static middleware - Mini Project: Build & Apply

Choose your learning style9 modes available
Serving Static Files with express.static Middleware
📖 Scenario: You are building a simple web server using Express.js. You want to serve static files like images, CSS, and JavaScript from a folder called public. This will allow users to access these files directly through the browser.
🎯 Goal: Create an Express server that uses the express.static middleware to serve files from the public folder.
📋 What You'll Learn
Create an Express app
Use express.static middleware to serve static files from the public folder
Listen on port 3000
💡 Why This Matters
🌍 Real World
Web servers often need to serve images, stylesheets, and scripts to browsers. Using express.static middleware makes this easy and efficient.
💼 Career
Knowing how to serve static assets is a fundamental skill for backend developers working with Node.js and Express.
Progress0 / 4 steps
1
Set up Express app
Write code to import express and create an Express app called app.
Express
Need a hint?

Use require('express') to import Express and call it to create app.

2
Create public folder path
Create a constant called publicPath that stores the string 'public' representing the folder name for static files.
Express
Need a hint?

Just assign the string 'public' to publicPath.

3
Use express.static middleware
Use app.use with express.static(publicPath) to serve static files from the public folder.
Express
Need a hint?

Call app.use and pass express.static(publicPath) as the argument.

4
Start the server
Add code to make the app listen on port 3000 using app.listen(3000).
Express
Need a hint?

Use app.listen(3000) to start the server on port 3000.