sai Kodavali
sai Kodavali

Reputation: 1

I created a small app using node js with express

The app is Gpa calculator using node js with express.But there is problem,it works fine for single user but when any other user doing same in same time,then the marks are getting combined and showing wrong values.I want build app which works separately for each user without combining the values.

const express=require("express");
const https=require("https");
const bodyParser=require("body-parser");
const mongoose=require("mongoose");
const lr=require("livereload");



const app=express();

app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine","ejs");
var list=[0];
var clist=[0];
var len=0;
var sum=0;
var csum=0;
var fin;
var per;






app.get("/",function(req,res){
    res.render("index",{len:len});
});

app.post("/",function(req,res){
    var cr=req.body.crd;
    var sc=req.body.scr;
    var ccr=parseInt(cr);
    if(sc=="S"){
        sc=10;
    }
    else if(sc=="A"){
        sc=9;
    }
    else if(sc=="B"){
        sc=8;
    }
    else if(sc=="C"){
        sc=7;
    }
    else if(sc=="D"){
        sc=6;
    }
    else{
        sc=0;
        len=parseInt(len)-1;
        res.send("Give valid inputs.");
    }

    var tot=parseInt(cr)*parseInt(sc);
    len=list.length;
    clist.push(ccr);
    list.push(tot);
    res.redirect("/");
    
});


app.post("/calculate",function(req,res){
    for(var i=0;i<list.length;i++){
        sum+=list[i];
    }
    for(var j=0;j<clist.length;j++){
        csum+=clist[j];
    }
    fin=sum / csum;
    var finn=fin.toString();
    var finnn=finn.slice(0,4);
    per=fin*9.5;
    var perr=per.toString();
    var perrr=perr.slice(0,5);

    
    res.render("display",{fin:finnn, per:perrr});
})
app.post("/reset",function(req,res){
    list=[0];
    clist=[0];
    len=0;
    sum=0;
    csum=0;

    res.redirect("/");
})



var ser=app.listen(process.env.PORT || 3000,function(){
    console.log("server is running on port 3000");
    
});
<%- include("bootstrap") %>
<!DOCTYPE html>
<html>
<head>
    <title>Hello</title>
     
</head>
<body style="background-color: #fffbdf;">


    <nav style="margin-bottom: 2%;" class="navbar navbar-expand-lg navbar-light bg-dark">
  <a style="color: white;" class="navbar-brand" href="#">Veltech Calculator</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
    <div class="navbar-nav">
      
    </div>
  </div>
</nav>
    
        <center>
        <form action="/" method="post">
            
            <label><h6>Enter Credits :</h6></label>
            <input id="crd" placeholder="Enter credits" style="margin: 2%" type="text" name="crd" required autofocus>
            <label><h6>Enter Grade : </h6></label>
            <input id="scr" placeholder="Enter grade in CAPITAL" style="margin: 2%" type="text" name="scr" required="Fill this" autofocus><br>
            <% if(len==0){%>
            <h5 style="color: red; margin-bottom: 2%;">No subject is added</h5>

        
        <% } else{ %>
            <h5 style="color: green; margin-bottom: 2%;"><%= len %> subjects are are added</h5>
        <% } %>

        
        <button type="submit" id="add" class="btn btn-warning" style="width: 80px;">ADD</button><br>
        <div style="margin-bottom: 0.5%; margin-top: 2%; display:inline-block;" class="alert alert-danger" role="alert">
        <b>NOTE</b>: If you done anything wrong while adding subjects,try to click on RESET button.Else may get wrong values. 
        </div>
        </form>
        <form  action="/calculate" method="post">
        <button type="submit" id="cal" class="btn btn-primary btn-lg" style="margin-top: 3%; width: 200px;">Calculate</button>
        </form>
        <form  action="/reset" method="post">
            <button type="submit" id="reset" class="btn btn-secondary btn-lg" style="margin-top: 1%; width: 200px;">RESET</button>
        </center>

        <center>
<div style="background-color: #4ca1a3; margin-top: 2%" id="cf">
    <h5 id="cfh" style="padding-top: 1%; color: #0c4271">Veltech Calculator</h5>
    <label><b>Website links :</b></label>
        <a style="padding-bottom: 1%; color: black;" href="https://www.veltech.edu.in">Veltech official site</a>
    
    <h6 style="padding-bottom: 1%;"> @VeltechCalculator copy rights reserved/leela sairam</h6>

    
</div>
</center>

</body>
</html>
<%- include("bootstrap") %>

Upvotes: 0

Views: 145

Answers (1)

Quentin
Quentin

Reputation: 943630

Store your user-specific data in a session instead of in global variables.

A session works by generating a unique ID for each visitor and storing it in a cookie. It has a central data store (which could just be an in-memory object or could be a file or database) which associates whatever data you like with that unique ID. When you want to read or write to it, you just get the ID from the cookie and look up the data based on it.

The express-session module will do all the heavy lifting for you.

Upvotes: 1

Related Questions