I don"t understand why there is something wrong with mongodb"s connection after writing a login interface. As long as a login request is sent, after the request ends, its service will exit directly.
when starting the service, connect to the database:
const mongoose = require("mongoose");
const db = require("../db/config/key").mongoURI;
//connect db
mongoose.connect(db,{ useNewUrlParser: true }).then( () => console.log("DB Successful")).catch((err) => console.log(err));
here is my login interface:
/**
* authenticate()3nameoptions
*/
router.post("/login",urlencodedParser,(req,res,next) => {
const loginUser = {
email:req.body.email,
password:req.body.password
};
console.log(loginUser);
passport.authenticate("local", {
successRedirect:"/",
failureRedirect: "/admin/login",
failureFlash: true
})(req, res, next);
// UserSchema.findOne({
// email:loginUser.email
// }).then(users => {
// console.log("user" + users)
// if(!users){
// return done(null, false, {message: "No User Found"});
// }
// //
// bcrypt.compare(loginUser.password, users.password, (err, isMatch) => {
// if(err) throw err;
// if(isMatch){
// res.redirect("/")
// } else {
// res.redirect("/admin/login")
// }
// })
// })
});
passport.js
const UserSchema = require("../models/UserSchema");
module.exports = function(passport){
passport.use(new LocalStrategy({usernameField: "email"}, (email, password, done) => {
console.log(":" + email+"/"+ password)
// Match user
UserSchema.findOne({
email:email
}).then(users => {
console.log("user" + users)
if(!users){
return done(null, false, {message: "No User Found"});
}
// Match password
bcrypt.compare(password, users.password, (err, isMatch) => {
console.log("password " + password);
console.log("user.password" + users.password);
if(err) throw err;
if(isMatch){
console.log("isMatch " + isMatch)
// token
// const rule = {
// id: users._id,
// email:users.email,
// admin:"true"
// };
// jwt.sign(rule,secretKey,{expiresIn:3600},(err,token) => {
// if(err) throw err;
// res.json({
// sucess:true,
// token:"Bearer " + token
// });
// });
return done(null, users);
} else {
return done(null, false, {message: "Password Incorrect"});
}
})
})
}));
as long as you make a request, you will exit directly, and then there will be such a prompt in the window, which will be broken again.
{ MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (E:\API\anviz\anviz\web\node_modules\mongodb-core\lib\topologies\server.js:564:11)
at Pool.emit (events.js:182:13)
at Connection.<anonymous> (E:\API\anviz\anviz\web\node_modules\mongodb-core\lib\connection\pool.js:317:12)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at Socket.<anonymous> (E:\API\anviz\anviz\web\node_modules\mongodb-core\lib\connection\connection.js:246:50)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: "MongoNetworkError",
errorLabels: [ "TransientTransactionError" ],
[Symbol(mongoErrorContextSymbol)]: {} }
I have mongodb configured locally and a service to add mongodb in windows.
when the computer is started, the mongodb service is also started. The configuration in windows startup service is: E:APIDBbinmongod.exe-- bind_ip 192.168.70.181-- logpath E:APIDBlogmongodb.log-- logappend-- dbpath E:APIDBdatadb-- port 27017-- service
but every time the service starts, it must be manually typed: mongod.exe-- dbpath E:APIDBdatadb will start successfully, and then the manually started service can only be used once, and the request will be cut off directly?
May I ask what the reason is?very anxious, thank you very much!