简体   繁体   English

我使用 node js 和 express 创建了一个小应用程序

[英]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.该应用程序是使用 node js 和 express 的 Gpa 计算器。但是有问题,它适用于单个用户,但是当任何其他用户同时做同样的事情时,标记被合并并显示错误的值。我想要构建可以工作的应用程序分别为每个用户而不组合值。

 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") %>

Store your user-specific data in a session instead of in global variables.将用户特定数据存储在 session 中,而不是全局变量中。

A session works by generating a unique ID for each visitor and storing it in a cookie. session 的工作原理是为每个访问者生成一个唯一 ID 并将其存储在 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.它有一个中央数据存储(可以只是内存中的 object 或者可以是文件或数据库),它将您喜欢的任何数据与该唯一 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.当您想读取或写入它时,您只需从 cookie 中获取 ID 并根据它查找数据。

The express-session module will do all the heavy lifting for you. express-session模块将为您完成所有繁重的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM