не проходит авторизация
Помогите разобраться, не проходит авторизация !
'use strict';
let http = require('http');
let url = require('url');
let fs = require('fs');
let query = require('querystring');
let mongodb = require('mongodb').MongoClient;
let logged = {};
let customPaths = [
'/login',
'/logout',
'/register',
'/get'
];
let onlyLogged = [
'/index.html'
];
let mongopath = 'mongodb://localhost:27017/db';
let redirect = (res, address) => {
res.writeHead(302, {
'Location': address
});
res.end();
};
let processFile = (res, address) => {
if (fs.existsSync(__dirname + address)) {
res.statusCode = 200;
res.end(fs.readFileSync(__dirname + address));
} else {
redirect(res, '/404.html');
}
};
let login = (req, res, params) => {
if (params.user && params.pass) {
let dict = {
user: params.user,
pass: params.pass
};
mongodb.connect(mongopath, (err, db) => {
if (!err) {
let collection = db.collection('dbvaiscoin');
collection.find(dict).toArray((error, docs) => {
if (!error) {
if (docs.length !== 0) {
logged[String(req.connection.remoteAddress)] = params.user;
res.statusCode = 200;
res.end();
} else {
res.statusCode = 400;
res.end();
}
}
});
} else {
res.statusCode = 500;
res.end();
}
db.close();
});
}
};
let logout = (req, res) => {
logged[String(req.connection.remoteAddress)] = undefined;
res.statusCode = 200;
res.end();
};
let getData = (req, res) => {
mongodb.connect(mongopath, (err, db) => {
if (!err) {
let collection = db.collection('dbvaiscoin');
collection.find(logged[String(req.connection.remoteAddress)]).toArray((error, docs) => {
if (!error) {
res.statusCode = 200;
res.end(JSON.stringify(docs[0]));
} else {
res.statusCode = 500;
res.end();
}
})
} else {
res.statusCode = 500;
res.end();
}
db.close();
})
};
let register = (req, res, params) => {
if (params.user, params.pass) {
mongodb.connect(mongopath, (err, db) => {
let collection = db.collection('dbvaiscoin');
let doc = {
user: params.user,
pass: params.pass,
money: 0,
dollars: 0,
celery: 1000
};
collection.insert(doc);
logged[String(req.connection.remoteAddress)] = {
user: params.user,
pass: params.pass
};
db.close();
});
}
};
let processCustomUrl = (res, req, params, uri) => {
if (uri === '/login') {
login(req, res, params);
} else if (uri === '/logout') {
logout(req, res);
} else if (uri === '/register') {
register(req, res, params);
} else if (uri === '/get') {
get(req, res);
}
};
let acceptRequest = (req, res)=> {
let uri = url.parse(req.url).pathname;
let params = query.parse(url.parse(req.url).query);
if (uri.length <= 1) {
uri = '/index.html';
}
if (uri in customPaths) {
processCustomUrl(res, req, params, uri);
} else {
if (onlyLogged.indexOf(uri) !== -1 && !logged[String(req.connection.remoteAddress)]) {
redirect(res, '/login.html');
} else {
processFile(res, uri);
}
}
console.log(logged);
};
let interval = () => {
mongodb.connect(mongopath, (err, db) => {
if (!err) {
let collection = db.collection('dbvaiscoin');
collection.find({}).toArray((error, docs) => {
docs.forEach((doc) => {
collection.update(doc, {
$inc: {
money: doc.celery
}
});
db.close();
});
});
}
});
};
setInterval(interval, 10000);
http.createServer(acceptRequest).listen(8080);
console.log('Server Started!');
|