const express = require('express');
const router = express.Router();
const games = [
{ src: "./routes/assets/tlou.png", game: "The Last of Us", creator: "Naughty Dog" },
{ src: "./assets/god.jfif", game: "God Of War", creator: "Sony" },
{ src: "./assets/battlefield.jfif", game: "Battlefield V", creator: "Eletronic Arts" },
{ src: "./assets/cod.webp", game: "Call Of Duty Black Ops II", creator: "Actvision" },
{ src: "./assets/dbx.webp", game: "Dragon Ball Xenoverse", creator: "Bandai Namco" },
{ src: "./assets/fifa22.webp", game: "FIFA 22", creator: "Eletronic Arts" },
{ src: "./assets/fall.jpeg", game: "Fall Guys", creator: "Epic Games" },
{ src: "./assets/gta.webp", game: "GTA V", creator: "Rockstar Games" },
{ src: "./assets/sonic.jfif", game: "Sonic Hedgehog", creator: "Nintendo" },
]
router.get("/games", (req, res) => {
let gamesNames = ""
games.forEach((games) => {
gamesNames = `<div style="display: flex; flex-direction:column;"><img src="${games.src}" height="200px"><h2>${games.game}</h2> <i style="margin-top:-15px;">for ${games.creator}</i></div> <br>`
})
res.send(gamesNames)
})
module.exports = router
CodePudding user response:
You need to use the express.static
middleware to serve static files.
Check the docs here: https://expressjs.com/en/starter/static-files.html
CodePudding user response:
You need a route to serve the image files. This is best done with express.static. Add this code before your module.exports
line and remove the leading .
from each of your src
properties.
router.use('/assets', express.static('assets'));