如何从NodeJsServer的查询字符串中获取id或任何变量数据
How to GET id or any variable data from Query String in Node Js Server?
我已经设置了一个节点Js服务器。现在我正在从数据库中获取所有用户的记录。
现在,如果我想要一个具有特定Id的特定用户,我如何将其传递给Node Js?
例如:我们在PHP中执行:$_GET['id'];我在NodeJS中也想要同样的东西
这是我的服务器代码:
var app = require('http').createServer(),
io = require('socket.io').listen(app),
fs = require('fs'),
mysql = require('mysql'),
connectionsArray = [],
connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'test',
port : 3306
}),
POLLING_INTERVAL = 1000,
pollingTimer;
// If there is an error connecting to the database
connection.connect(function(err) {
// connected! (unless `err` is set)
console.log( err );
});
// creating the server ( localhost:8000 )
app.listen(8000);
/*
*
* HERE IT IS THE COOL PART
* 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 );
}
});
socket.on('ddd', function (name) {
socket.username=name;
});
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 );
});
};
首先,您必须为客户端嵌入socket.io JavaScript:
script(rel="text/javascript" src="/js/libraries/socket.io.min.js");
然后你像这样连接到你的套接字服务器:
var socket = io.connect("https://localhost:8080");
连接后,您可以启动事件:
button.on("click", function(){
socket.emit("event-name", { userId : 2 });
});
在服务器端,您有相应的侦听器:
socket.on("event-name", function(data){
console.log(data.userId); // logs: 2
var query = // ...
socket.emit("response", query);
});
再次在客户端,您检索响应:
socket.on("response", function(data){
console.log(data); // logs: the query
});
要确定哪个客户端应该接收发射,请参阅:
socket.io房间还是命名空间?
授权请参见:
https://github.com/LearnBoost/socket.io/wiki/Authorizing
相关文章:
- 使用angularjs中的rest调用通过id获取数据
- javascript从id获取数字
- 如何通过ID获取图层对象
- 通过jstree中的Id获取节点
- 在控制器中按ID获取XML ViewElement(MultiInput)
- node.js使用monk从数组中按id获取文档
- 如何使用API从多个IMDB ID获取电影信息
- 我的ID获取元素不起作用
- 如何通过 id 获取“this”元素并查看是否选中
- 使用类而不是 ID(通过 CLASS 而不是 ID 获取元素)
- 如何在Ember.js中使用jQuery通过id获取元素
- 使用类名按ID获取元素
- jquery模态对话框通过id获取输入类型text的值
- 通过ID获取声明性dojo数据网格
- 可以't按id获取元素
- 根据DropDown ID获取DropDownListFor的选定值
- jquery如何使用chlid id获取父元素的类
- 在iframe中通过id获取元素
- 主干模型通过ID获取JSON元素
- 在javascript中通过id获取元素,而页面上没有任何表单标记