简体   繁体   中英

Why my log in page load slowly when i login

Can someone please help me withmy login route? When I click login , the page just loads slowly and then return error.

It never go to the page that I want to redirect to or render the page when I'm logged in. I have deleted some of my code for privacy reasons. You guys can use your own database for testing. Any help would be appreciated. PS, i have updated the code to try a diffrent method

const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const { nextTick } = require('process');

const app = express();

app.use(bodyParser.urlencoded({extended: false}));

// mongoose.connect('mongodb+srv://apiuser:abcd1234@cluster0.rcyha.mongodb.net/users?retryWrites=true&w=majority');

// const userSchema = new mongoose.Schema({
//     email: String,
//     password: String
// });

let objUser = {
    username: 'aiman',
    password: '123'
}


// const User = new mongoose.model("User", userSchema);

app.get('/', function(req,res){
    res.sendFile(__dirname + '/home.html');
});

app.get('/loan', function(req,res){
    res.sendFile(__dirname + '/loanform.html');
})

app.get('/repayment', function(req,res){
    res.sendFile(__dirname + '/repayment.html');
});

app.get('/login', function(req,res){
    res.sendFile(__dirname + '/login.html');
});

app.get('/register', function(req,res){
    res.sendFile(__dirname + '/register.html');
});

app.post('/login', function(req,res, err){
    let username = req.body.username
    let password = req.body.password

    for(let i = 0; i < objUser.length; i++){
        if(objUser[i].username == username && objUser[i].password == password){
            res.redirect('/');
        } else{
            console.log(err);
        }
    }
});

app.listen(3001, function(req,res){
    console.log("Server started on port 3001");
});

The problem is that the client is waiting for an response, but if something fails on the serverside you does not send him an response back to say "hey something went wrong here" you only send the client an response if everything wents well.

That means that the client will wait and wait till he gets the error "maximum execution time exceeded" or something like that

app.post('/login', function(req,res){
    const email = req.body.email
    const password = req.body.password

   User.findOne({email: email}, function(err, foundUser){
       if(err){
           console.log(err);
           res.status(500).json({ message: "Something went wrong" })
       } else{
           if(foundUser){
               if(foundUser.password === password){
                   res.redirect('/repayment');
               }else {
                  //password not equal
                  res.status(400).json({ message: "Password arent equal" });
               }
           }else {
              //user not found 
              res.status(404).json({ message: "Cannot find user" })
           }
       }
   }); 
});

You also need to send back an response if something doesnt went how you expect. Same for your /register route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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