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

215
Views
Cómo hacer que Express genere una pregunta aleatoria desde mi JSON local

Tengo un servidor local que he creado para aprender y practicar mi codificación de back-end. Ahora mismo está en las primeras etapas. Mi código es solo una aplicación express básica, puedo solicitar el archivo json y puedo mostrarlo, pero lo que no estoy seguro de cómo hacer es cada vez que se actualiza la página para cargar una pregunta diferente.

aplicación.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}`) })

preguntas.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

Eso debería funcionar, es posible que desee definir primero la ruta de la matriz de preguntas en lugar de tenerla escrita a mano.

 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!