You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.5 KiB
91 lines
2.5 KiB
3 years ago
|
const app = require('express')();
|
||
|
const http = require('http').Server(app);
|
||
|
var path = require('path');
|
||
|
var io = require('socket.io')(http);
|
||
|
const MongoClient = require('mongodb').MongoClient;
|
||
|
|
||
|
|
||
|
app.get('/', (req, res) => {
|
||
|
res.send('<h1>Hello world!</h1>');
|
||
|
});
|
||
|
|
||
|
app.get('/test', (req, res) => {
|
||
|
|
||
|
|
||
|
var user="test1"
|
||
|
var pass="newpass"
|
||
|
//mongo “mongodb://localhost:30001,localhost:30002,localhost:30003/$MONGO_INITDB_DATABASE” -u $MONGO_INITDB_USERNAME mongo “mongodb://localhost:30001,localhost:30002,localhost:30003/app_swarmlab” -u app_swarmlab
|
||
|
//mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRs
|
||
|
//var mongourl = "mongodb://"+user+":"+pass+"@localhost:30001,localhost:30002,localhost:30003/app_swarmlab?replicaSet=rs0&authSource=admin"
|
||
|
/*
|
||
|
use admin
|
||
|
db.createUser(
|
||
|
{
|
||
|
user: "test1",
|
||
|
pwd: 'newpass', // Or "<cleartext password>"
|
||
|
roles: [ { role: "readWrite", db: "app_swarmlab" } ],
|
||
|
authenticationRestrictions: [ {
|
||
|
clientSource: ["192.168.1.7"],
|
||
|
serverAddress: ["192.168.80.2", "192.168.80.3", "192.168.80.4"]
|
||
|
} ]
|
||
|
}
|
||
|
)
|
||
|
*/
|
||
|
|
||
|
var mongourl = "mongodb://"+user+":"+pass+"@192.168.80.2:27017/app_swarmlab?replicaSet=rs0&authSource=admin"
|
||
|
const OPTS = {
|
||
|
useNewUrlParser: true,
|
||
|
useUnifiedTopology: true
|
||
|
};
|
||
|
var options = {
|
||
|
useNewUrlParser: true,
|
||
|
useUnifiedTopology: true
|
||
|
}
|
||
|
|
||
|
|
||
|
MongoClient.connect(mongourl, options, function(err, client){
|
||
|
if(err){
|
||
|
console.log(err);
|
||
|
} else {
|
||
|
console.log(JSON.stringify('mongo ----------------connected'))
|
||
|
const db = client.db('app_swarmlab');
|
||
|
db.collection('logs').find({}).toArray()
|
||
|
.then(item => {
|
||
|
console.log('item '+JSON.stringify(item))
|
||
|
for (let i in item) {
|
||
|
console.log(JSON.stringify('items' + item[i]))
|
||
|
}
|
||
|
})
|
||
|
|
||
|
}
|
||
|
})
|
||
|
res.send('<h1>test!</h1>');
|
||
|
|
||
|
});
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
io.on('connection', s => {
|
||
|
console.error('socket connection');
|
||
|
|
||
|
s.on('log', (data, room) => {
|
||
|
s.to('iot').emit('message', data);
|
||
|
console.log('broadcast', data);
|
||
|
|
||
|
});
|
||
|
//s.emit('message', 'message from server');
|
||
|
|
||
|
|
||
|
|
||
|
});
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
http.listen(8084, () => {
|
||
|
console.log('listening on *:8084');
|
||
|
});
|