-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
39 lines (29 loc) · 908 Bytes
/
server.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
const express = require('express')
const amqplib = require('amqplib')
const amqpUrl = process.env.AMQP_URL || 'amqp://localhost'
const app = express()
const port = 3000
const exch = 'api';
const queue = 'api-event'
const rkey = 'api.#'
let conn
let ch
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.get('/api/message', async (req, res) => {
const msg = { ...req.query, ts: new Date() }
await ch.publish(exch, 'api.message', Buffer.from(JSON.stringify(msg)));
res.send({ success: true })
})
async function main() {
conn = await amqplib.connect(amqpUrl);
ch = await conn.createChannel()
await ch.assertExchange(exch, 'topic', { durable: true })
await ch.assertQueue(queue, { durable: true });
await ch.bindQueue(queue, exch, rkey);
app.listen(port, () => {
console.log(`RabbitMQ app listening at http://localhost:${port}`)
})
}
main()