skip to Main Content

Im trying to deliver all images inside a assets folder using Node and Express.

Folder structure:

.
└── src/
    ├── assets/
    │   ├── 01.webp
    │   └── 02.jpg
    └── components/
        ├── Gamegrid.js
        └── testedir/
            ├── 01.webp
            └── 02.jpg

The Gamegrid.js code:

function definirGrid(numeroDeCartas = 12){

    console.log('hei')

    const express = require('express');
    const bodyParser = require('body-parser');
    const path = require('path');
    const app = express();
    const PORT = 3001;

    app.use('/testedir', express.static('testedir'));
    app.use(path.join(__dirname,'..','assets'), express.static('assets'));

    app.get('/',(req, res)=>{
        res.send('hello world');
    });
    app.get('/sobre',(req, res)=>{
        res.send('página sobre');
    });

    app.listen(PORT, ()=>{
        console.log('app listening on port',PORT)
    })
}

definirGrid()

I’m able to access http://localhost:3001/testedir/02.jpg but not http://localhost:3001/assets/02.jpg.

How can I route for the ../assets images?

2

Answers


  1. maybe like this:

    var url= 'http://localhost:3001/testedir/02.jpg';
    var new_url = url.replace(new RegExp('testedir','g'), 'assets');
    console.log(new_url);
    Login or Signup to reply.
  2. You can do it in the same way as you did with the testedir folder.

    app.use('/testedir', express.static('testedir'));
    app.use('/assets', express.static('../assets'));
    

    However, I would advise you, to create one public folder which may include all the other folders you want to serve in public.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search