Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
How to get Express to output random question from my local JSON

I have a local server that I have created to learn and practice my backend coding. Right now its in the early stages. My code is just a basic express app, i can require the json file in and i can display it but what im not sure how to do is every time the page is refreshed to load a different question?

app.js

const express = require('express')
const questions = require('./question.json')

const app = express()
const PORT = 3000

app.get('/', (req, res)=>{
    res.set('Content-Type', 'text/html');
    res.status(200).send("<h1>Hello</h1>");
})

app.listen(PORT, (error) =>{
        console.log(`Server is Successfully Running, and App is listening on port ${PORT}`)
})

questions.json

{
   "questions":[
      {
         "1":"Question 1?"
      },
      {
         "2":"Question 2?"
      },
      {
         "3":"Question 3?"
      },
      {
         "4":"Question 4?"
      }
   ]
}
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

That should work, you might want to define the path of the questions array first instead of having it written out long handed.

const express = require('express')
const questions = require('./questions.json')

const app = express()
const PORT = 3000

app.get('/', (req, res)=>{

    JSON.parse(JSON.stringify(questions))
    var randomObject = questions.questions[Math.floor(Math.random() * questions.questions.length)]
    console.log(randomObject)


    res.set('Content-Type', 'text/html');
    res.status(200).send("<h1>Hello GFG Learner!</h1>");
})

app.listen(PORT, (error) =>{
        console.log(`Server is Successfully Running, and App is listening on port ${PORT}`)
})
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda