这是一个使用nodejs,socket.io开发的实时聊天应用程序,消息在mongo数据库网站中使用特定URL保存在mongo数据库中。 我从heroku下载了nodejs的演示文件,它被称为node-js-getting-started并在javascript,css和view文件中更改以匹配应用程序但是当我推送它并打开控制台时我得到这个:无法加载资源:服务器响应状态为404(未找到)socket.io.js。
Index.js:
var express = require('express');
var path = require('path');
var app = express();
var http = require('http').Server(app);
var client = require('socket.io')(http);
var mongo = require('mongodb').MongoClient;
var url ="mongodb://<dbuser>:<dbpassword>@ds153752.mlab.com:53752/chatapp";
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
response.render('pages/index');
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
mongo.connect(url,function(err,db){
if(err)throw err;
client.on('connection', function(socket){
var col = db.collection('messages'),
sendStatus = function(s){
socket.emit('status',s);
};
//emit all messages
col.find().limit(200).sort({_id:1}).toArray(function(err,res){
if(err) throw err;
socket.emit('output',res);
});
// wait for input
socket.on('input',function(data){
var name = data.name,
message = data.message,
whitespacePattern = /^\s*$/;
if(whitespacePattern.test(name)||whitespacePattern.test(message)){
sendStatus('Name and Message is Required.');
}else{
col.insert({name:name,message:message},function(){
//emit latest message to all clients
client.emit('output',[data]);
sendStatus({
message : "Message Sent",
clear : true
});
});
}
});
});
});
Index.ejs :(查看)
<!DOCTYPE html>
<html>
<head>
<title> Chat App </title>
<link rel="stylesheet" href="/stylesheets/main.css">
</head>
<body>
<div class="chat">
<input type="text" class="chat-name" placeholder="Enter Your Name">
<div class="chat-messages" style="">
</div>
<textarea placeholder="Type Your Message"></textarea>
<div class="chat-status">Status : <span name="st" id ="st">Idle</span></div>
</div>
<script src="/socket.io/socket.io.js"></script>
<script>
(function(){
var getNode = function(s){
return document.querySelector(s);
},
textarea = getNode('.chat textarea'),
messages = getNode('.chat-messages'),
chatName = getNode('.chat-name'),
status = getNode('.chat-status span'),
statusDefault = status.textContent,
setStatus = function(s){
status.textContent = s;
if(s !== statusDefault){
var delay = setTimeout(function(){
setStatus(statusDefault);
clearInterval(delay);
},3000);
}
};
try{
var socket = io.connect('https://evening-springs-70834.herokuapp.com');
}catch(e){
}
if(socket !== undefined){
//listen for output
socket.on('output',function(data){
if(data.length){
for(var x=0;x<data.length;x++){
var message = document.createElement('div');
message.setAttribute('class','chat-message');
message.textContent = data[x].name + ' : ' + data[x].message;
message.style.marginBottom = 10 + 'px';
messages.insertBefore(message,messages.firstChild);
messages.appendChild(message);
messages.scrollTop = messages.scrollHeight;
}
}
});
//listen for status
socket.on('status',function(data){
setStatus((typeof data === 'object') ? data.message : data);
if(data.clear === true){
textarea.value = '';
}
});
textarea.addEventListener('keydown',function(event){
var text = this,
name = chatName.value;
if(event.which === 13 && event.shiftKey === false){
socket.emit('input',{
name : name,
message : text.value
});
event.preventDefault();
}
});
}
})();
</script>
</body>
</html>