ExpressJS static files

In ExpressJS, you can serve static files, such as images, stylesheets, and scripts, to the client. Here’s an example of a simple ExpressJS application that serves a static image of a car:

In this example, the app.use(express.static('public')) method is used to tell ExpressJS to serve files from the public folder as static files. The app.get('/', ...) route is used to render a simple HTML page that displays an image of a car by using an <img> tag with a src attribute set to /car.jpg.

Here’s an example of the folder structure for this ExpressJS application:

In this example, the my-express-app folder is the root directory of the application. The public folder contains the static files, and the car.jpg file is the image of the car. The app.js file contains the ExpressJS application code.

You can test this application by running the code and navigating to http://localhost:3000 in your web browser. You should see an image of a car displayed on the page.Regenerate response

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top