听特定的url,而不是端口

我对Nodejs是完全陌生的,并试图熟悉它。 那么,目前,我正尝试在Web浏览器上推送通知,如果在数据库中发生任何更改。 我有一块代码,我一直用于testing目的。 当数据库表内容更改时,以下代码可以正常工作,以便将更新发送

Server.js

var app = require('http').createServer(handler).listen(8000), io = require('socket.io').listen(app), fs = require('fs'), mysql = require('mysql'), connectionsArray = [], connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'nodejs', port: 3306 }), POLLING_INTERVAL = 3000, pollingTimer, pollingTimer_2; // If there is an error connecting to the database connection.connect(function(err) { // connected! (unless `err` is set) console.log(err); }); // on server started we can load our client.html page function handler(req, res) { fs.readFile(__dirname + '/client.html', function(err, data) { if (err) { console.log(err); res.writeHead(500); return res.end('Error loading client.html'); } res.writeHead(200); res.end(data); }); } /* * This function loops on itself since there are sockets connected to the page * sending the result of the database query after a constant interval * */ var pollingLoop = function() { // Doing the database query var query = connection.query('SELECT * FROM users'), users = []; // this array will contain the result of our db query // setting the query listeners query .on('error', function(err) { // Handle error, and 'end' event will be emitted after this as well console.log(err); updateSockets(err); }) .on('result', function(user) { // it fills our array looping on each user row inside the db users.push(user); }) .on('end', function() { // loop on itself only if there are sockets still connected if (connectionsArray.length) { pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL); updateSockets({ users: users }); } }); }; // creating a new websocket to keep the content updated without any AJAX request io.sockets.on('connection', function(socket) { console.log('Number of connections:' + connectionsArray.length); // starting the loop only if at least there is one user connected if (!connectionsArray.length) { pollingLoop(); } socket.on('disconnect', function() { var socketIndex = connectionsArray.indexOf(socket); console.log('socket = ' + socketIndex + ' disconnected'); if (socketIndex >= 0) { connectionsArray.splice(socketIndex, 1); } }); console.log('A new socket is connected!'); connectionsArray.push(socket); }); var updateSockets = function(data) { // adding the time of the last update data.time = new Date(); // sending new data to all the sockets connected connectionsArray.forEach(function(tmpSocket) { tmpSocket.volatile.emit('notification', data); }); }; 

Client.html

 <html> <head> <title>Push notification server streaming on a MySQL db</title> <style> dd,dt { float:left; margin:0; padding:5px; clear:both; display:block; width:100%; } dt { background:#ddd; } time { color:gray; } </style> </head> <body> <time></time> <div id="container">Loading ...</div> <script src="socket.io/socket.io.js"></script> <script src="http://code.jquery.com/jquery-latest.min.js"></script> <script> // create a new websocket var socket = io.connect('http://localhost:8000'); // on message received we print all the data inside the #container div socket.on('notification', function (data) { var usersList = "<dl>"; $.each(data.users,function(index,user){ usersList += "<dt>" + user.user_name + "</dt>\n" + "<dd>" + user.user_desc + "\n" + "<figure> <img class='img-polaroid' width='50px' src='" + user.user_img + "' /></figure>" "</dd>"; }); usersList += "</dl>"; $('#container').html(usersList); $('time').html('Last Update:' + data.time); }); </script> </body> </html> 

现在,您可以看到当前服务器正在port 8000处进行侦听。 我只是想知道如何改变它来听一个特定的url? 因为如果我要在服务器项目上实现,那么我不会使用该URL来监听端口? 而是我想用它作为简单的Url,因为我可以顺利发送通知,如果任何用户连接到特定的url?

任何帮助?

如果你希望你的节点服务器响应URL http://www.example.com/updates ,那么你可以让你的服务器监听端口80(默认端口,如果没有端口列出,原型是“http”)。 然后,你让你的服务器响应"/updates"路线。

服务器侦听特定IP地址的特定端口。 他们不听path或URL。 浏览器使用DNS在URL中获取主机的IP地址。 如果URL中有端口号,则使用该端口。 如果不是,则使用特定协议的默认端口。 然后在该端口上build立到该IP地址的TCP连接。 然后,如果该协议是http,它将在该连接上发送一个请求并包含该path。

因此,服务器接收传入的连接,然后作为HTTP协议的一部分,它接收动词(GET,POST,DELETE等),然后是服务器作业的path,以决定基于传入的命令/path。