[Fixed] How to use GET method in Expressjs

Issue

Person, I have a folder called "vehicles" on my PC, and I want to create a localhost to create a link for each image of that.

Example:

localhost: 800 / vehicle / <name of the .png file>

For when I access the link, I return the image.

However, I am new … I would like to assist you in how I can do this.

I did some things but always return to 404 not found error status.

tentative code:


const express = require('express')
const app = express()
const port = 3000

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

app.get("/", (req, res) => {
  let img = `<img src="./vehicles/150.png"/>`;
  let html = `<!Doctype html><html><head><title>Sample</title></head>`;
  html += `<body><main>${img}</main></body></html>`;
  res.send(html);
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

enter image description here

I’m a beginner, I would like to know how I can do this …

Solution

If you want you can place thats files in folder named "public/vehicles" and then express will do everythig for you.

Folder public is place for sharing files like js, css and images etc.

Leave a Reply

(*) Required, Your email will not be published