Giter Club home page Giter Club logo

random-user-genarator's Introduction

Random User Generator

Welcome to my first ever backend-only project using Software Architectural Pattern. Here I used MVC (Module View Controller). The messy backend code I wrote in the past has become so much pretty that its like a dream. I love MVC. Kudos to Trygve Mikkjel Heyerdahl Reenskaug for inventing such an amazing pattern.

Live Heroku Server
Live Vercel Server (on development)

Technologies

  • node
  • express
  • cors
  • axios
  • heroku

APIs

https://floating-fortress-65518.herokuapp.com/api/v1/user   Base URL

1. /all               gives all users and limit number of users by using query
  - https://floating-fortress-65518.herokuapp.com/api/v1/user/all
  - https://floating-fortress-65518.herokuapp.com/api/v1/user/all?num=5
  
2. /random            gives a random user
  - https://floating-fortress-65518.herokuapp.com/api/v1/user/random

3. /save              saves a new user
  - https://floating-fortress-65518.herokuapp.com/api/v1/user/save

4. /update?query      updates the user with the id
  - https://floating-fortress-65518.herokuapp.com/api/v1/user/update?id=4

5. /delete?query      deletes a user
  - https://floating-fortress-65518.herokuapp.com/api/v1/user/delete?id=3

6. /bulk-update       updates a number of users

Problems

  1. When hit on the /random route, with and without query, first time data shows perfectly. If hit multiple times with and without query the expected result is not shown. Query is used for limiting the number of users.
    This problem arises when I read the users file once at the starting of the users.controllers.js file.

Problem code

const data = fs.readFileSync("users.json");
const users = JSON.parse(data);

module.exports.getAllUser = (req, res) => {
if (req.query.num === undefined) {
res.send(users);
} else {
const usersToBeShown = req.query.num;
const result = users.splice(0, usersToBeShown);
res.send(result);
}
};

But if I read the file every time using getUser() function the problem is solved.
I solved it on my own. But can not understand why it's happening.

Solved code

function getUsers() {
  const data = fs.readFileSync("users.json");
  const users = JSON.parse(data);
  return users;
}

module.exports.getAllUser = (req, res) => {
  if (req.query.num === undefined) {
    const users = getUsers();
    console.log("hit", users);
    res.send(users);
  } else {
    const users = getUsers();
    const usersToBeShown = req.query.num;
    const result = users.splice(0, usersToBeShown);
    console.log("hit with query");
    res.send(result);
  }
};

random-user-genarator's People

Contributors

thakursaad avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.