我第一次使用node.js而且我遇到了问题,因为它正在刷新其中一个get方法( app.get(' / addToHistory' ) )每2分钟没有任何迭代从我的身边或浏览器。可能问题是在我的server.js配置的某处,但我找不到它。
我正在尝试使用AngularJS,NodeJS和MySQL构建SPA。将有3页:主页(带有一些图像),图像页面(用户可以点击主页上的图像查看有关此图像的详细信息)和历史记录(用户看到的所有图像)。没有任何登录功能。应用程序通过会话ID识别用户,会话ID保存到数据库。
我正在使用angular的路由来在不同的页面之间导航。
这是我的server.js
的配置var mysql = require('mysql');
var cookieParser = require('cookie-parser');
var express = require('express');
var app = module.exports = express();
var session = require('express-session');
var MySQLStore = require('express-mysql-session')(session);
app.use(cookieParser());
var db_options = {
host: 'localhost',
//port: 3306,
user: 'user',
password: 'password',
database: 'myDB'
};
var option = {
host: 'localhost',
//port: 3306,
user: 'user',
password: 'password',
database: 'myDB',
schema: {
tableName: 'sessions',
columnNames: {
sessions_id: 'sessions_id',
expires: 'expires',
data: 'data'
}
}
};
var method = db.prototype;
function db() {
var con = mysql.createPool(db_options);
this.connection = con;
}
method.getcon = function () {
return this;
};
module.exports = db;
var connection_object = new db();
var db_connection = connection_object.connection;
app.get('/load', function (req, res) {
console.log('load');
db_connection.query("SELECT * from sessions", function (err, rows) {
if (err) {
console.log("Problem with MySQL" + err);
}
else {
res.end(JSON.stringify(rows));
}
});
});
app.get('/addToHistory', function (req, res) {
//THIS METHOD IS RECALLING EVERY 2 MINUTES
console.log('addToHistory');
var imageID = req.query.imageID;
var sessionID = req.cookies['image-session'];
sessionID = sessionID.substring(sessionID.indexOf(':') + 1, sessionID.indexOf('.'));
checkIfExist();
function checkIfExist(){
db_connection.query('SELECT * FROM `history` WHERE `session_id`="'+sessionID+'" AND `image_id`="'+imageID+'"', function (error, rows) {
if (error) {
console.log('Problem with MySQL' + error);
}else{
if(rows.length >= 1){
updateExisting();
}else{
createNew();
}
}
})
}
function createNew() {
db_connection.query('INSERT INTO `history` (`session_id`, `image_id`) VALUES ("' + sessionID + '", "' + imageID + '")', function (error, rows) {
if (error) {
console.log('Problem with MySQL' + error);
}else{
}
})
}
function updateExisting(){
db_connection.query('UPDATE history SET created=NOW() WHERE `session_id`="'+sessionID+'" AND `image_id`="'+imageID+'"', function(error, rows){
if (error) {
console.log('Problem with MySQL' + error);
}else{
}
})
}
});
var connection = mysql.createConnection(db_options);
var sessionStore = new MySQLStore(option, connection);
app.use(session({
name: 'image-session',
secret: 'thisidasnkjcmsxhodsjafasd89fdashojnva',
cookie: {maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: false, secure: false},
resave: true,
saveUninitialized: true,
store: sessionStore
}));
app.use('/js', express.static(__dirname + '/app/js'));
app.use('/bower_components', express.static(__dirname + '/app/bower_components'));
app.use('/css', express.static(__dirname + '/app/css'));
app.use('/partials', express.static(__dirname + '/app/partials'));
app.use('/views', express.static(__dirname + '/app/views'));
app.use('/components', express.static(__dirname + '/app/components'));
app.use('/images', express.static(__dirname + '/app/images'));
app.use(function (req, res) {
console.log(req.session.id)
res.sendFile(__dirname + '/app/index.html')
});
app.listen(process.env.PORT || 8080, function () {
console.log("Server is listening ");
});
当用户点击某些图像时,它正在打开一个新页面(状态),其中一个控制器正在调用工厂功能。这是我的工厂:
app.factory('dbCommunication', ['$http', function ($http) {
var obj = {};
obj.addToHistory = function(image){
console.log('call');
$http.get('http://localhost:8080/addToHistory?imageID='+image.id).success(function(data){
})
return;
}
return obj;
}]);
任何想法为什么node.js每2分钟重新加载 app.get(' / addToHistory' ?
顺便说一句。我想问一下你对我的server.js配置怎么看?可以吗?我应该换一些东西吗?我不确定将SessionID保存到cookie中是否是个好主意。