错误:连接丢失:服务器关闭了连接

时间:2019-05-20 07:51:29

标签: node.js

我刚刚将我的node.js应用程序放置在本地服务器中以进行托管,因此某些用户将能够使用它,而我遇到了以下问题:

当我运行该应用程序时,一切都运行良好,即使几分钟后它仍然可以正常运行。但是,如果我让它运行几天,它将停止工作,并且出现此错误。例如,我在最后一个星期五运行它,而今天(星期一)我远程连接到服务器时看到了此错误。

这是我的服务器文件:

const express = require('express');
const bodyParser = require("body-parser");
const crypto = require('crypto');
var session = require('express-session');
const internalIp = require('internal-ip');
const getPrices = require('./scrapers/getPrices.js');
const dbConnection = require('./dbConnection.js');
const app = express();
app.use('/assets', express.static('./assets/'));
const localIP = internalIp.v4.sync();
var hotelNames = [];
var urlsHotels = [];
var usersList = [];
var userInfo = new Object();

userInfo.id = undefined;
userInfo.role = 0;

app.use(session({
    secret: 'secret',
    resave: true,
    saveUninitialized: true
}));

app.get('/', function (req, res) {
    if (req.session.loggedin && userInfo.role == 0) {
        getHotelsdata(res);
    } else if (req.session.loggedin && userInfo.role == 1) {
        loadAdminPage(res);
    } else {
        res.redirect('/login');
    }
});

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

app.get('/login',function(req,res){
    globalRes = res
    //res.sendFile(__dirname + "/views/login.html");
    res.render('./login.ejs', {
        'localIP':localIP,
    });
});
app.post('/submitlogin',function(req,res){
    const password = crypto.createHash('sha256').update(req.body.password).digest('hex');
    const users = new Promise((resolve, reject) => {
        dbConnection
        .getUsers()
        .then(data => {
            resolve(data)
        })
    });
    Promise.all([users])
    .then(data => {
        usersList = data[0];
        var loginSuccess = "0";
        for(var i = 0; i < data[0].length; i++) {
            if(data[0][i][1] == req.body.user && data[0][i][2] == password) {    
                req.session.loggedin = true;
                req.session.username = req.body.user;
                loginSuccess = "1";
                userInfo.id = data[0][i][0];
                userInfo.role = data[0][i][3];
            }
        }
        res.end(loginSuccess);
    });
});

app.post('/deleteUser', function(req, res) {
    const deleteUser = new Promise((resolve, reject) => {
        dbConnection
        .deleteUser(req.body.user)
        .then(data => {
            resolve(data)
            response = data[0];
        })
    });
    Promise.all([deleteUser])
    .then(data=> {
        getUsersData(res, response);
    });
})

app.post('/changePwd', function(req, res) {
    const currentPassword = crypto.createHash('sha256').update(req.body.currentPassword).digest('hex');
    const newPassword = crypto.createHash('sha256').update(req.body.newPassword).digest('hex');
    const changePwd = new Promise((resolve, reject) => {
        dbConnection
        .changePwd(currentPassword, newPassword)
        .then(data => {
            resolve(data)
            response = data[0];
        })
    });
    Promise.all([changePwd])
    .then(data=> {
        res.end(data[0]);
    });
})

app.post('/addUser', function(req, res) {
    const password = crypto.createHash('sha256').update(req.body.password).digest('hex');
    var response;
    const insertUser = new Promise((resolve, reject) => {
        dbConnection
        .registerUser(req.body.user, password)
        .then(data => {
            resolve(data)
            response = data;
        })
    });
    Promise.all([insertUser])
    .then(data => {
        getUsersData(res, response);
    });
});

app.get('/compare/:visitDay&:visitMonth&:visitYear&:leavingDay&:leavingMonth&:leavingYear', async function(req, res, next) {
    if (req.session.loggedin) {
        var results = new Array();
        const date1 = new Date(req.params.visitMonth + '-' + req.params.visitDay + '-' + req.params.visitYear);
        const date2 = new Date(req.params.leavingMonth + '-' + req.params.leavingDay + '-' + req.params.leavingYear);
        const timeDiff = Math.abs(date2.getTime() - date1.getTime());
        const nights = Math.ceil(timeDiff / (1000 * 3600 * 24));
        assignurls(req.params.visitYear, req.params.visitMonth, req.params.visitDay, req.params.leavingYear, req.params.leavingMonth, req.params.leavingDay);
            const getPrices = new Promise((resolve, reject) => {
            getPrices 
            .getPrices(results, nights, urlsHotels)
            .then(data => {
                resolve(data)
            })
        });
        Promise.all([getPrices])
        .then(data => {
            loadPage(res, JSON.stringify(hotelNames), JSON.stringify(results), req.params.visitDay + "/" + req.params.visitMonth + "/" + req.params.visitYear, req.params.leavingDay + "/" + req.params.leavingMonth + "/" + req.params.leavingYear);
        });
    } else {
        res.redirect('/login');
    }    
});

app.get('/addHotel&:url&:name', function(req, res, next) {
    if (req.session.loggedin) {
        let url = req.params.url.split('');
        url.splice(44, 3);
        url[44] = 'visitYear';
        url.splice(46, 1);
        url[46] = 'visitMonth';
        url.splice(48, 1);
        url[48] = 'visitDay';
        url.splice(70, 3);
        url[70] = 'leavingYear';
        url.splice(72, 1);
        url[72] = 'leavingMonth';
        url.splice(74, 1);
        url[74] = 'leavingDay';
        url = url.join('');
        const insertHotel = new Promise((resolve, reject) => {
            dbConnection
            .insertValues(hotelNames, url, req.params.name, userInfo.id)
            .then(data => {
                resolve(data)
            })
        });
        Promise.all([insertHotel])
        .then(data => {
            getHotelsdata(res, data);
        });
    } else {
        res.redirect('/login');
    }
});

app.get('/deleteHotel&:name', function(req, res, next) {
    if (req.session.loggedin) {
        const deleteHotel = new Promise((resolve, reject) => {
            dbConnection
            .deleteValues(req.params.name)
            .then(data => {
                resolve(data)
            })
        });
        Promise.all([deleteHotel])
        .then(data => {
            getHotelsdata(res, data);
        });
    } else {
        res.redirect('/login');
    }
});

app.get('/logout', function(req, res, next) {
    req.session.loggedin = false;
    res.redirect('/');
});

function assignurls(visitYear, visitMonth, visitDay, leavingYear, leavingMonth, leavingDay) {
    for(var i = 0; i<urlsHotels.length; i++) {
        var finalurl = urlsHotels[i].replace('visitYear', visitYear);
        finalurl = finalurl.replace('visitMonth', visitMonth);
        finalurl = finalurl.replace('visitDay', visitDay);
        finalurl = finalurl.replace('leavingYear', leavingYear);
        finalurl = finalurl.replace('leavingMonth', leavingMonth);
        finalurl = finalurl.replace('leavingDay', leavingDay);
        urlsHotels[i] = finalurl;        
    }
}

function getUsersData(res, response) {
    const users = new Promise((resolve, reject) => {
        dbConnection
        .getUsers()
        .then(data => {
            resolve(data);
        })
    });
    Promise.all([users])
    .then(data => {
        usersList = data[0];            
        res.end(response);
    });  
}

function getHotelsdata(res, results) {
    const getNotelNames = new Promise((resolve, reject) => {
        dbConnection
        .getHotelNames(userInfo.id)
        .then(data => {
            resolve(data)
        })
    });

    const getURLs = new Promise((resolve, reject) => {
        dbConnection
        .getURLs(userInfo.id)
        .then(data => {
            resolve(data)
        })
    });
    Promise.all([getNotelNames, getURLs])
    .then(data => {
        hotelNames = data[0];
        urlsHotels = data[1];
        loadPage(res, JSON.stringify(hotelNames), JSON.stringify([results]));
    });
}

function loadPage(res, param1, param2, param3, param4) {
    res.render('./comparatorPage.ejs', {
        'hotelNames':param1,
        'results': param2,
        'checkInDate': param3,
        'checkOutDate': param4,
        'localIP': localIP,
    });
}

function loadAdminPage(res) {
    res.render('./adminPage.ejs', {
        'usersList': JSON.stringify(usersList),
        'localIP': localIP
    });
}

app.listen(process.env.PORT || 3000);

那是我得到的错误:

正如我所说,一切正常,直到几天之内可以运行。

我该如何解决? Node.js是一种服务器端语言,因此不应发生这种情况。

谢谢。

其他信息:我使用Windows Server 2008 R2 Standard。

0 个答案:

没有答案