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

219
Views
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 answers
Answer question

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 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!