2014-02-20 166 views
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> 
+0

[Running a Node.js in apache?](http://stackoverflow.com/questions/14369865/running-node-js-in-apache) –

回答

1

您將需要使用在httpd.conf文件中配置的反向代理。我使用nginx來做到這一點,因爲它比apache2更快,更強大。

這是我nginx的配置文件:

server { 
    listen 80; 
    server_name personal.dev.x.onmylemon.co.uk; 

    access_log /var/log/nginx/dev.x.oml.access.log; 
    error_log /var/log/nginx/dev.x.oml.error.log; 

    location/{ 
     # This is the url to your node app on the localhost 
     proxy_pass    http://localhost:30450; 

     proxy_set_header  Host $host; 
     proxy_set_header  X-Real-IP $remote_addr; 
     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 

     proxy_connect_timeout 150; 
     proxy_send_timeout  100; 
     proxy_read_timeout  100; 

     proxy_buffers   4 32k; 

     client_max_body_size 8m; 
     client_body_buffer_size 128k; 
    } 
} 

沒有爲在Apache中建立一個反向代理一些文檔here

相關問題