简体   繁体   中英

Could not login using MongoDB and Node.js

I could not login user using MongoDB and Node.js.Its not throwing any error but could not provide any data which user want after login.I am explaining my code below.

server.js

var port=8888;
var express=require('express');
var morgan = require('morgan');
var http=require('http');
var bodyParser= require('body-parser');
var methodOverride = require('method-override');
var mongo = require('mongojs');
var database='Oditek';
var collections=['video'];
var app= express();
var server=http.createServer(app);
var io=require('socket.io')(server);
var db = mongo("127.0.0.1:27017/"+database, collections);
app.use(express.static(__dirname + '/public'));     // set the static files location /public/img will be /img for users
app.use(morgan('dev'));                     // log every request to the console
app.use(bodyParser.urlencoded({ extended: false }))    // parse application/x-www-form-urlencoded
app.use(bodyParser.json())    // parse application/json
app.use(methodOverride());                  // simulate DELETE and PUT
app.get('/',function(req,res){
    res.sendfile('view/login.html');
});
app.post('/login',function(req,res){
    var username=req.body.username;
    var password=req.body.userpassword;
    if(username && password){
        db.video.findOne({
            username:username,
            password:password
        },function(err,doc){
            console.log('login',doc);
            if(doc){
            res.send(doc);
            }
            if(err){
                console.log('login',err);
                res.send("could not login");
            }
        });
    }
});
app.get('/video',function(req,res){
    res.sendfile('view/video.html');
});
server.listen(port);
console.log('server is listening on the port'+port);

login.js:

function login(){
    var uName=$('#uname').val();
    var uPass=$('#upass').val();
    var data={username:uName,userpassword:uPass};
    $.ajax({
        type: 'POST',
        url:"/login",
        data:data,
        success: function(data){
            console.log(data);
            location.href="http://localhost:8888/video?usertype='+data.type+' & id= '+data.id+' ";
        }
    });
}

Package.json:

{
  "name": "oditek",
  "main": "server.js",
  "dependencies": {
    "express": "~4.0.0",
    "morgan": "~1.0.0",
    "body-parser": "~1.0.0",
    "method-override": "~1.0.0",
    "socket.io":"1.3.5",
    "mongojs": "1.2.1"
  }
}

The console part of the app.post method in server side is not executing at all.Please help me to resolve this error.

I have tested your code and its working fine for me, returning user record from video collection. Can you please check mongodb is running and you can also see by adding these lines.

    db.runCommand({ping: 1}, function (err, res) { if(!err && res.ok) console.log('we\'re up')});

I have tested your code and returning me fine, try to change this:

    app.use(bodyParser.urlencoded({ extended: true}));    // parse            application/x-www-form-urlencoded
    app.use(bodyParser.json()); 

Please also check in logs, morgan is writing logs on not when you hit url, please first check with postman(chrome plugin) to test your api.

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