-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicro_first.js
51 lines (41 loc) · 1.34 KB
/
micro_first.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const micromq = require('micromq');
const sqlite3 = require('sqlite3').verbose();;
const { RABBIT_URL } = require('./config');
const db = new sqlite3.Database('mydatabase.db');
const app = new micromq({
name: 'users',
rabbit: {
url: RABBIT_URL,
},
});
app.get('/friends', (req,res) => {
db.all("SELECT id, name FROM users", (err, rows) => {
if(err){
console.error(err.message);
res.status(500).json({error: "Ошибка обработки сервера"});
}else {
res.json(rows);
}
});
});
app.get('/status', (req, res) => {
res.json({
text: 'Thinking...',
});
});
app.get('/new_friends',(req,res) => {
const {id,name} = req.query;
if (!id || !name) {
return res.status(400).json({error:"Нет Id или name"});
}
db.run("INSERT INTO users (id,name) VALUES (?,?)", [id,name],function(err) {
console.error(err);
return res.status(500).json({error:'Ошибка записи на сервере'});
})
res.json({message: "Клиент добавлен"});
})
app.on('friends', async (message, properties, actions) => {
console.log('Получено сообщение из RabbitMQ:', message);
actions.send({ text: 'Сообщение успешно обработано' });
});
app.start();