3
我該如何在apache服務器上運行我的node.js代碼,該服務器當前正在使用本地主機和端口5000到apache服務器端口8080. 這是我的服務器端代碼聊天服務器。 jsNode.js服務器從localhost更改爲apache
var httpd = require('http').createServer(handler);
var io = require('socket.io').listen(httpd);
var fs = require('fs');
httpd.listen(5000);
function handler(req, res) {
fs.readFile(__dirname + '/index.html',
function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
}
);
}
io.sockets.on('connection', function (socket) {
socket.on('clientMessage', function(content) {
socket.emit('serverMessage', 'You said: ' + content);
socket.broadcast.emit('serverMessage', socket.id + ' said: ' +
content);
});
});
這是我的客戶端代碼。 index.html
<html>
<head>
<title>Node.js WebSocket chat</title>
<style type="text/css">
#input {
width: 200px;
}
#messages {
position: fixed;
top: 40px;
bottom: 8px;
left: 8px;
right: 8px;
border: 1px solid #EEEEEE;
padding: 8px;
}
</style>
</head>
<body>
Your message:
<input type="text" id="input">
<div id="messages"></div>
<script src="http://localhost:5000/socket.io/socket.io.js"></script>
<script type="text/javascript">
var messagesElement = document.getElementById('messages');
var lastMessageElement = null;
function addMessage(message) {
var newMessageElement = document.createElement('div');
var newMessageText = document.createTextNode(message);
newMessageElement.appendChild(newMessageText);
messagesElement.insertBefore(newMessageElement,
lastMessageElement);
lastMessageElement = newMessageElement;
}
var socket = io.connect('http://localhost:5000');
socket.on('serverMessage', function(content) {
addMessage(content);
});
var inputElement = document.getElementById('input');
inputElement.onkeydown = function(keyboardEvent) {
if (keyboardEvent.keyCode === 13) {
socket.emit('clientMessage', inputElement.value);
inputElement.value = '';
return false;
} else {
return true;
}
};
</script>
</body>
</html>
[Running a Node.js in apache?](http://stackoverflow.com/questions/14369865/running-node-js-in-apache) –