I try to code the last exercise in the node tutorial. But, I meet some issue when I run the app. My browser (chrome), throw "Cannot POST /" on the window, and in the console I get "Failed to load resource: the server responded with a status of 404 (Not Found)". Here what my code looks like:
-server:
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
ent = require('ent'), // For security purpose
// Loading the web page
app.get('/', function(req, res) {
res.sendFile('views/index.html', {root : __dirname});
});
io.sockets.on('connection', function(socket, username) {
// When the username is received, it's stored as a session variable
//and we inform the other people
socket.on('new-client', function(username) {
username = ent.encode(username);
socket.username = username;
socket.broadcast.emit('new-client', username);
});
// When a message is received, the username is retrieved and sent to other people
socket.on('message', function(message) {
message = ent.encode(message);
socket.broadcast.emit('message', {username: socket.username, message: message});
});
});
server.listen(8080);
- client
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Super Chat</title>
<meta name="description" content="My first super chat with socket.io">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form #message { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form #send-message { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#chat-zone p { margin: 0; padding: 0; }
#chat-zone p { padding: 5px 10px; }
#chat-zone p:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<h1>REAL-TIME SUPER CHAT!</h1>
<section id="chat-zone"></section>
<form action="/" method="post" id="chat-form">
<input type="text" name="message" id="message" autofocus>
<input type="submit" id="send-message" value="send">
</form>
<!------------------------------------------------------------------------------------------>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
// Connection to socket.io
var socket = io.connect('http://localhost:8080');
var chatZone = document.getElementById('chat-zone');
var chatForm = document.getElementById('chat-form');
var chatMessage = document.getElementById('message');
var username = prompt('Your Pseuso');
socket.emit('new-client', username);
document.title = username + '-' + document.title;
// When a message is received, we display it in the chat-zone
socket.on('message', function(data) {
insertMessage(data.username, data.message)
})
// When a client is connected, we display his name in the chat-zone
socket.on('new-client', function(username) {
chatZone.prependChild('<p><em>' + username + ' has joined the chat!</em></p>')
})
// When we send our form, the message is received, displayed on our page and sent to other people
chatForm.submit(function() {
var message = chatMessage.getElementValue();
socket.emit('message', message);
insertMessage(username, message);
chatMessage.innerHTML('').focus();
});
// Get an element value
HTMLElement.prototype.getElementValue = function() {
var text = this.innerText || this.textContent;
return text;
};
// Prepend element in the DOM
HTMLElement.prototype.prependChild = function(childNode) {
this.firstChild ? this.insertBefore(childNode, this.firstChild) : this.appendChild(childNode);
};
// Add message to the chat-zone
function insertMessage(username, message) {
chatZone.prependChild('<p><strong>' + username + '</strong>' + message + '</p>');
}
</script>
</body>
</html>
I work on it three days long. Can someone help fix it? Thanks
Normally if you copy/paste correctly the code of the tutorial, it work good, after i'm not good in node.js but if i look the code in the tutorial you have forgot to declare the fs variable in server code ?
fs = require('fs');
- Edité par Milan_0512 13 janvier 2016 à 22:31:47
Node.js and Socket.io
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.