Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda