DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones Build AI Agents That Are Ready for Production
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
Build AI Agents That Are Ready for Production

Just dropped: New 2026 “Cloud-Native Foundations” Trend Report. See how teams are tackling complexity, cost & reliability.

AI can investigate. Engineers still decide. See how both work together across incident response in this DZone + Datadog webinar on Oct. 29.

Related

  • Exploring the DBMS_DEVELOPER Package: JSON Metadata for Oracle Objects
  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Part 2: Securing and Scaling Goose-to-Java Agent Traffic With agentgateway

Trending

  • Can Your Team Name the Work It Already Runs With AI?
  • Testing Business Programs Without Constructing Domain Objects
  • Building a Practical Cloud-Native Golden Path: A Guide to Kubernetes-Based Service Delivery, Self-Service, and Developer-Friendly Defaults
  • MCP Is the USB-C of AI — Here's What That Actually Means for Your Architecture
  1. DZone
  2. Coding
  3. JavaScript
  4. Fetching Information Randomly From JSON Using Node, Nuxt, Express

Fetching Information Randomly From JSON Using Node, Nuxt, Express

Nuxt.js, Node.js and Express code to filter out data based on user requirement and select data randomly from the filtered data.

By 
Richard Davis user avatar
Richard Davis
·
Sep. 10, 26 · Analysis
Likes (2)
Comment
Save
Tweet
Share
3.5K Views

Join the DZone community and get the full member experience.

Join For Free

Nuxt.js is a popular framework for Vue.js, and it is widely used for websites that require server-side rendering. It is similar to the Next.js framework for React.js. In this article, I’m going to share how you can fetch values randomly from a static JSON file with a Node and Express server. 

To make this example more realistic, we will store some words with their meanings in the words.json file in a static folder at the root. The necessary frameworks and libraries need to be installed on your machine, and basic knowledge is required:

  • Node.js/ express
  • Vue.js/Nuxt.js CLI
  • JSON
  • npm

Random words list

(Source)


#static/words.json

JSON
 
[
  {
    "word": "lysis",
    "type": "noun",
    "meaning": "The resolution or favorable termination of a disease, coming on gradually and not marked by abrupt change."
  },
  {
    "word": "outwit",
    "type": "verb",
    "meaning": "To surpass in wisdom, esp. in cunning; to defeat or overreach by superior craft."
  },
  {
    "word": "completive",
    "type": "adjective",
    "meaning": "Making complete."
  }
]


The words.json file above contains a few words, each with its type and meaning. 

Next, we need an Express server that listens for API calls from the front Nuxt/Vue page.

#server.js 

JavaScript
 
const path = require('path');
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const app = express();
const PORT = 3001;

app.use(cors());

let words = JSON.parse(fs.readFileSync('static/words.json', 'utf-8'));

words = words.map(w => ({
  ...w,
  type: w.type ? w.type.trim().toLowerCase() : ''
}));


app.get('/api/types', (req, res) => {
  const uniqueTypes = [...new Set(words.map(w => w.type))].sort();
  res.json(uniqueTypes);
});

//Random word generator with filters
app.get('/api/random', (req, res) => {
  let filtered = [...words];
  const { type, start, end, op, len, count } = req.query;

  const requestedType = type ? type.trim().toLowerCase() : '';
  const startLetter = start ? start.trim().toLowerCase() : '';
  const endLetter = end ? end.trim().toLowerCase() : '';
  const wordLength = len ? parseInt(len) : null;
  const limit = parseInt(count) || 5;

  if (startLetter) {
    filtered = filtered.filter(w => w.word?.toLowerCase().startsWith(startLetter));
  }

  if (endLetter) {
    filtered = filtered.filter(w => w.word?.toLowerCase().endsWith(endLetter));
  }

  if (requestedType && requestedType !== 'all') {
    filtered = filtered.filter(w => w.type === requestedType);
  }

  if (op && wordLength) {
    if (op === '=') filtered = filtered.filter(w => w.word.length === wordLength);
    else if (op === '<') filtered = filtered.filter(w => w.word.length < wordLength);
    else if (op === '>') filtered = filtered.filter(w => w.word.length > wordLength);
  }

  const result = [];
  const available = [...filtered];
  while (result.length < limit && available.length > 0) {
    const index = Math.floor(Math.random() * available.length);
    result.push(available.splice(index, 1)[0]);
  }

  res.json(result);
});

app.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}`);
});


As the Nuxt.js server runs on port 3000 by default, we have specified port number 3001.

Next up is the Vue/nuxt.js code. With an input selection form and a “generate words” button.

#pages/index.vue

Vue.js Component
 
        

Random Words List

Your generated words will appear here.

  • {{ word.word }} ({{ word.type }})

    {{ word.meaning }}

This is the normal HTML form that will be placed inside