Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
Permitir todas las imágenes de la carpeta de imágenes en la lista allowURLs

Creé una lista de URL permitidas para los usuarios que no han iniciado sesión, pero tengo que definir cada imagen en la carpeta si el usuario no ha iniciado sesión. ¿Hay alguna forma de que todos los archivos de la carpeta de imágenes estén permitidos para los usuarios que no ¿conectado?

 app.use((req, res, next) => { // console.log(req.session.user) let userLoggedIn = req.session.user !=null let allowedURLs = [ '/html/login.html', '/css/style.css', '/js/login.js', '/api/users/login', '/js/script.js', '/html/footer.html', '/favicon.ico', '/html/home.html', '/html/nav.html', '/html/footer.html', <---- HERE IS MY ISSUE---> `/img/logo.jpeg`, <--- HERE IS MY ISSUE ---> '/html/shows.html', '/js.shows.js', '/api/shows', ] let adminOnlyURLS = [ ] if (userLoggedIn) { // let them through if (adminOnlyURLS.includes(req.originalUrl) && req.session.user.accessRights != "admin") { res.redirect("/html/home.html"); } else { next() } } else { if (allowedURLs.includes(req.originalUrl)) { //allows the guest user through next() } else { //if not allowed - reditect to the login page res.redirect("/html/home.html") } } })

entre AQUÍ ESTÁ MI PROBLEMA Necesito que todas las imágenes estén disponibles para el usuario que no haya iniciado sesión. Este es mi archivo app.js

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede cambiar sus URL permitidas a una matriz de cadenas o expresiones regulares

Entonces el if cambia a

 if (allowedURLs .some(url => url.test ? url.test(req.originalUrl) : url === req.originalUrl) ) {

Vea el código completo a continuación

 app.use((req, res, next) => { // console.log(req.session.user) let userLoggedIn = req.session.user != null; let allowedURLs = [ '/html/login.html', '/css/style.css', '/js/login.js', '/api/users/login', '/js/script.js', '/html/footer.html', '/favicon.ico', '/html/home.html', '/html/nav.html', '/html/footer.html', /^\/img\/.*/, // this is a RegExp '/html/shows.html', '/js.shows.js', '/api/shows', ]; let adminOnlyURLS = [ ]; if (userLoggedIn) { // let them through if (adminOnlyURLS.includes(req.originalUrl) && req.session.user.accessRights != "admin") { res.redirect("/html/home.html"); } else { next(); } } else { if (allowedURLs.some(url => url.test ? url.test(req.originalUrl) : url === req.originalUrl)) { //allows the guest user through next(); } else { //if not allowed - reditect to the login page res.redirect("/html/home.html"); } } });

Otra posibilidad es

 let allowedURLs = [ '/html/login.html', '/css/style.css', '/js/login.js', '/api/users/login', '/js/script.js', '/html/footer.html', '/favicon.ico', '/html/home.html', '/html/nav.html', '/html/footer.html', /^\/img\/.*/, // this is a RegExp '/html/shows.html', '/js.shows.js', '/api/shows', ].map(v => new RegExp(v.replaceAll?.('.', '\\.') ?? v))

entonces el si puede ser

 if (allowedURLs.some(url => url.test(req.originalUrl))) {
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!