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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
const express = require('express') const app = express() app.use(express.static('public')) app.get('/', (req, res) => { res.send('<img src="/car.jpg" alt="A car">') }) app.listen(3000, () => { console.log('Example app listening on port 3000!') }) |
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:
1 2 3 4 5 |
my-express-app/ public/ car.jpg app.js |
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