我的Node.Js服务器工作正常,但经过一天的自行停止!
请帮我。
我使用以下命令运行服务器:
$forever start index.js
我的索引代码是:
var fs = require('fs');
var http = require('http').Server(app);
var https = require('https');
var privateKey = fs.readFileSync('key.txt', 'utf8');
var certificate = fs.readFileSync('Certificate.txt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
var httpsServer = https.createServer(credentials, app);
var io = require('socket.io')(httpsServer); // web socket external module
var bodyParser = require('body-parser');
process.on('uncaughtException', function (err) {
console.error('uncaughtException', err.stack);
});
app.use(bodyParser.urlencoded({extended: true}))
app.get('/newpost', function (req, res) {
res.end(new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ' ') + ' users : ' + Object.keys(io.of("/").adapter.nsp.connected).length);
});
app.post('/newpost', function (req, res) {
res.end();
io.emit('newdata', req.body);
});
httpsServer.listen(3000, function () {
console.log('listening on *:3000');
});
和客户端javascript代码是:
var socket = io('https://www.example.com:3000');
socket.on('newdata', function (newPost) {
console.log(newPost);
var tr = $('<tr>');
var td1 = '<td class="pforumtitle"><a href="' + newPost.pbburl + '/forumdisplay.php?' + newPost.pforumid + '"><div>' + newPost.pforumtitle + '</div></a></td>';
var td2 = '<td class="ptime"><a href=""><div>' + newPost.ptime + '</div></a></td>';
tr.html(td1 + td2)
$('#list').prepend(tr);
$('#list tr:last').remove();
});
永远的logfile: