I'm facing problems connecting express to Rabbit MQ - javascript

On visiting local host 3000 I get Cannot GET / before using postman.
during the process of sending POST request in postman to the local host my program fails and I get this Error
`node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
Error: connect ECONNREFUSED ::1:5672
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 5672`
config.js
----------
module.exports = {
rabbitMQ: {
url: "amqp://localhost",
exchangeName: "logExchange",
},
};
Producer.js
-----------
const amqp = require("amqplib");
const config = require("./config");
//step 1 : Connect to the rabbitmq server
//step 2 : Create a new channel on that connection
//step 3 : Create the exchange
//step 4 : Publish the message to the exchange with a routing key
class Producer {
channel;
async createChannel() {
const connection = await amqp.connect(config.rabbitMQ.url);
this.channel = await connection.createChannel();
}
async publishMessage(routingKey, message) {
if (!this.channel) {
await this.createChannel();
}
const exchangeName = config.rabbitMQ.exchangeName;
await this.channel.assertExchange(exchangeName, "direct");
const logDetails = {
logType: routingKey,
message: message,
dateTime: new Date(),
};
await this.channel.publish(
exchangeName,
routingKey,
Buffer.from(JSON.stringify(logDetails))
);
console.log(
`The new ${routingKey} log is sent to exchange ${exchangeName}`
);
}
}
module.exports = Producer;
Server.js
----------
`const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const Producer = require("./producer");
const producer = new Producer();
app.use(bodyParser.json("application/json"));
app.post("/sendLog", async (req, res, next) => {
await producer.publishMessage(req.body.logType, req.body.message);
res.send();
});
app.listen(3000, () => {
console.log("Server started...");
});`
I am a Junior Intern ad was following along an youtube video by https://www.youtube.com/watch?v=igaVS0S1hA4 by Computerix to help me understand the concepts
I tried restarting the rabbitMQ and express server and running it on different ports but didnt solve the problem

This is Big Picture
Postman -> Express Server -> RabbitMQ -> node.js
#1 Postman send message to Express with Message Types and payload by POST call
#2 Express Server forward message to Rabbit MQ by publish
#3 RabbitMQ - push the messages into own message queue
#4 Each consumer received own message from RabbitMQ. It was binned before.
There are many complex options(order, priority) but in here talking about basic and simple model.
Steps
We needs following three steps.
#1 : Install
1.1 docker (easy way to container to run RabbitMQ)
1.2 node
1.3 demo source
git clone https://github.com/charbelh3/RabbitMQ-Logger-Example.git
File structure
1.4 install node library
At RabbitMQ-Logger-Example\infoMS directory
npm install amqplib
At RabbitMQ-Logger-Example\loggerMS directory
npm install amqplib express body-parser
At RabbitMQ-Logger-Example\warningAndErrorMS directory
npm install amqplib
1.5 Install Postman
1.6 Install Git Bash - I will using git bash terminal. It similar bash terminal in Linux - If you using Linux, skip this.
#2 : Binding
2.1 running RabbitMQ
Save this code as docker-compose.yml
version: "3.8"
services:
rabbitmq:
image: rabbitmq:management-alpine
container_name: 'rabbitmq'
ports:
- 5672:5672
- 15672:15672
volumes:
- ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
- ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
networks:
- rabbitmq_go_net
networks:
rabbitmq_go_net:
driver: bridge
Run it
docker compose up
If docker compose version 1.x
docker-compose up
Open your browser with this URL
http://localhost:15672/
2.2 Run express and all node.js
At RabbitMQ-Logger-Example\infoMS directory
node app.js
At RabbitMQ-Logger-Example\loggerMS directory
node server.js
At RabbitMQ-Logger-Example\warningAndErrorMS directory
node app.js
Now ready to go
#3 : Message
Run Postman,
#1 Make own collection
#2 Make Three requests
One of Request - Send Info Message
#1 Select POST method
#2 Enter URL
localhost:3000/sendLog
#3 Click Body Tab
#4 Select Raw radio button
#5 Enter message into body
{
"logType" : "Info",
"message" : "I am Info message"
}
#6 Select JSON
#7 Press Send Button
If you see , You are success!
Other two different type message is same content
Just difference is logType's value.
Send Warning Message Request
For Warning
{
"logType" : "Warning",
"message" : "I am Warning message"
}
Send Error Message Request
For Error
{
"logType" : "Error",
"message" : "I am Error message"
}
This is real screen all of things!
If you browser, RabbitMQ content, you found many things.
This is one of it for Info channel information.
If want to terminate RabbitMQ
CTRL+C
docker compose down
Enjoy RabbitMQ!

Related

Node deploy failing because container did not respond to warmup request

I'm deploying a node app to azure and the deploy is failing because the container is not responding to the warmup request. The app starts locally, and is listening on the same port as the warmup requests. Here are the deploy logs:
2022-10-29T06:01:45.287Z INFO - Initiating warmup request to container
customcalligraphy_0_9c31f200 for site customcalligraphy
2022-10-29T06:03:45 No new trace in the past 1 min(s).
2022-10-29T06:04:45 No new trace in the past 2 min(s).
2022-10-29T06:05:45 No new trace in the past 3 min(s).
2022-10-29T06:01:45.035657849Z _____
2022-10-29T06:01:45.035686150Z / _ \ __________ _________ ____
2022-10-29T06:01:45.035691350Z / /_\ \\___ / | \_ __ \_/ __ \
2022-10-29T06:01:45.035695650Z / | \/ /| | /| | \/\ ___/
2022-10-29T06:01:45.035699650Z \____|__ /_____ \____/ |__| \___ >
2022-10-29T06:01:45.035703750Z \/ \/ \/
2022-10-29T06:01:45.035707650Z A P P S E R V I C E O N L I N U X
2022-10-29T06:01:45.035711351Z
2022-10-29T06:01:45.035714951Z Documentation: http://aka.ms/webapp-linux
2022-10-29T06:01:45.035718651Z NodeJS quickstart: https://aka.ms/node-qs
2022-10-29T06:01:45.035722451Z NodeJS Version : v18.2.0
2022-10-29T06:01:45.035726151Z Note: Any data outside '/home' is not persisted
2022-10-29T06:01:45.035729951Z
2022-10-29T06:01:45.152855802Z Starting OpenBSD Secure Shell server: sshd.
2022-10-29T06:01:45.215494733Z Starting periodic command scheduler: cron.
2022-10-29T06:01:45.235215447Z Cound not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml'
2022-10-29T06:01:45.242910847Z Could not find operation ID in manifest. Generating an operation id...
2022-10-29T06:01:45.242928347Z Build Operation ID: feba110f-d139-44e2-b66c-0e10cff5c53d
2022-10-29T06:01:45.476505931Z Environment Variables for Application Insight's IPA Codeless Configuration exists..
2022-10-29T06:01:45.522707434Z Writing output script to '/opt/startup/startup.sh'
2022-10-29T06:01:45.595152421Z Running #!/bin/sh
2022-10-29T06:01:45.595192122Z
2022-10-29T06:01:45.595198422Z # Enter the source directory to make sure the script runs where the user expects
2022-10-29T06:01:45.595204922Z cd "/home/site/wwwroot"
2022-10-29T06:01:45.595210723Z
2022-10-29T06:01:45.595215423Z export NODE_PATH=/usr/local/lib/node_modules:$NODE_PATH
2022-10-29T06:01:45.595220223Z if [ -z "$PORT" ]; then
2022-10-29T06:01:45.595224923Z export PORT=8080
2022-10-29T06:01:45.595229723Z fi
2022-10-29T06:01:45.595234423Z
2022-10-29T06:01:45.595239023Z npm start
2022-10-29T06:01:47.149225225Z npm info it worked if it ends with ok
2022-10-29T06:01:47.149401829Z npm info using npm#6.14.15
2022-10-29T06:01:47.150028044Z npm info using node#v18.2.0
2022-10-29T06:01:47.346984068Z npm info lifecycle custom-calligraphy-ecommerce#1.0.0~prestart: custom-calligraphy-ecommerce#1.0.0
2022-10-29T06:01:47.348375300Z npm info lifecycle custom-calligraphy-ecommerce#1.0.0~start: custom-calligraphy-ecommerce#1.0.0
2022-10-29T06:01:47.352639898Z
2022-10-29T06:01:47.352662299Z > custom-calligraphy-ecommerce#1.0.0 start /home/site/wwwroot
2022-10-29T06:01:47.352681799Z > node index.js
2022-10-29T06:01:47.352686199Z
2022-10-29T06:01:48.466127815Z STARTING CUSTOM CALLIGRAPHY SERVER
2022-10-29T06:01:49.692607132Z CCvbeta1.1 Listening on port 8080!
2022-10-29T06:01:50.344268374Z serving app
2022-10-29T06:02:21.332686762Z serving app
2022-10-29T06:02:52.341610061Z serving app
2022-10-29T06:03:23.343535160Z serving app
2022-10-29T06:03:54.350896814Z serving app
2022-10-29T06:04:25.361345562Z serving app
2022-10-29T06:04:56.364076228Z serving app
2022-10-29T06:05:27.370947666Z serving app
2022-10-29T06:05:57.383Z ERROR - Container customcalligraphy_0_9c31f200 for site customcalligraphy did not start within expected time limit. Elapsed time = 252.0962469 sec
2022-10-29T06:05:57.401Z ERROR - Container customcalligraphy_0_9c31f200 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2022-10-29T06:05:57.408Z INFO - Stopping site customcalligraphy because it failed during startup.
My app is actually running, but the deploy is failing. Here is my serverside code:
console.log("STARTING CUSTOM CALLIGRAPHY SERVER");
const express = require("express");
const path = require("path");
const app = express();
app.use(express.static(path.join(__dirname, "frontend", "build")), () =>
console.log("serving app")
);
app.use(express.json({ limit: "5gb" }));
const port = process.env.port || 8080;
const version = "beta1.1";
app.listen(port, () => console.log(`CCv${version} Listening on port ${port}!`));
How can I disable the warmup request or respond to the pings? Any help would be appreciated.
I would suggest use of express-generator to create the app. Just run
npx express-generator myapp
Then you can add your code in the app.js file.
my app file looks like this
console.log("STARTING CUSTOM CALLIGRAPHY SERVER");
const express = require("express");
const path = require("path");
const app = express();
app.use(express.static(path.join(__dirname, "frontend", "build")), () =>
console.log("serving app")
);
app.use(express.json({ limit: "5gb" }));
const port = process.env.port || 8080;
module.exports = app;
Here instead of listening to the ports in the app.js file we export the app entity and all the listening of port will be done in the www file.
This www also call the app.js file and will run app.
Now you can just deploy the app by you preferred way of choice. Here I have used vscode.
This way you will not get the could not find javascript build manifest file errors
logs after deployment of app :
Also use port 80 as it is already exposed in the app service.

Node Segmentation Fault for any node command with very basic code

Why am I getting a segmentation fault error whenever I type in any node commands?
A bit of background information: I'm trying to deploy any basic demo node app (in GoDaddy's shared hosting) following these instructions (from the comment from user called 'x.g.'). I do everything and get to the very last instruction (number 5) where it states to type node app.js & and I get the following response in the terminal:
[1] 326516
If I type node app.js without the ampersand & I get this error:
Segmentation fault
Does anyone know why this happens and how to fix it? I basically have (as per the instructions) an app.js with this simple code:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('NodeJS server running on Shared Hosting\n');
});
server.listen(port, hostname, () => {
console.log('Server running at http://${hostname}:${port}/');
});
Also, whenever I type anything like node, npm or node-v it also states the Segmentation fault error. Any help would be much appreciated!
Update: any ideas anyone?

how to get the port number from the service name using nodejs

We are developing a WebSocket server using Nodejs with socket.io
I have used createServer method from class HTTP and then listen to a port as follows :
var server = http.createServer();
server.listen(port, function () {
...
}
Now The problem is that I only have the service name
and listening to a service doesn't work :
var server = http.createServer();
server.listen(service, function () {
...
}
So I need to read and parse the file /etc/services to get the port associated with the service as follows :
var str = fs.readFileSync(filename).toString();
var serviceline = str.match( port+".*tcp" );
var port = serviceline[0].match( "[0-9]+" )
Is there a simpler way to get the port from the service?
Thanks in advance
Jean-Marie
You can use find-process to find port from process or vice-versa
Command line usage
npm install find-process -g
Usage: find-process [options] <keyword>
Options:
-V, --version output the version number
-t, --type <type> find process by keyword type (pid|port|name)
-p, --port find process by port
-h, --help output usage information
Examples:
$ find-process node # find by name "node"
$ find-process 111 # find by pid "111"
$ find-process -p 80 # find by port "80"
$ find-process -t port 80 # find by port "80"
Code usage
npm install find-process --save
const find = require('find-process');
find('pid', 12345)
.then(function (list) {
console.log(list);
}, function (err) {
console.log(err.stack || err);
})
I have used this to find process from the port but I think it provides a reverse way to or you can skim down to its repo and pick the code you needed.
I don't need a server port but the port corresponding to a service name
for instance having the following service "wsgw" specified in file /etc/services
wsgw 8001/tcp # websocket gateway
I need to get the value 8001 before running the server listening to this port this way :
var server = http.createServer();
server.listen(8001, function () {
...
listening to a service as below doesn't work :
var server = http.createServer();
server.listen("wsgw", function () {
...
note : finding a port from a process doesn't help me as it is not a process but a service

mosquitto+mqtt.js got "Connection refused: Not authorized"

I built mosquitto on CentOS7 and a node.js client based on mqtt.js,installing with
yum install mosquitto mosquitto-clients
The local test
> mosquitto_sub -h localhost -t test
> mosquitto_pub -h localhost -t test -m "hello world"
works fine, but when I ran:
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://192.168.1.70')
client.on('connect', function () {
client.subscribe('presence')
client.publish('presence', 'Hello mqtt')
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
client.end()
})
I got Error: Connection refused: Not authorized
The mosquitto.conf is like:
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
allow_anonymous true
and I use systemctl restart mosquitto to restart it several time, which doesn't help. The firewall is down and log file stays empty.
A screenshot on status:
Can anyone help please?
UPDATE:
It turns out that the mosquitto service is somehow broken as the status shows Active: active (exited).
I use mosquitto -p 1884 -v cmd to run another mosquitto process on port 1884, it works fine. Then I try to reload the conf using
> /etc/init.d/mosquitto reload. It gives me
Reloading mosquitto configuration (via systemctl): Job for mosquitto.service invalid.
[FAILED]
So there IS something wrong with mosquitto service.
Not a final solution but I manage to fix this by remove-reboot-install process, the status went green as follow:
SOLUTION
I managed to find out the reason it doesn't work. I've installed rabbitmq on my server, it uses its "rabbitmq_mqtt" which consumes port 1883. Reassigning a port will solve this problem.
I managed to find out the reason. I've installed rabbitmq on my server, it uses its "rabbitmq_mqtt" which consumes port 1883. Reassigning a port will solve this problem. The problem is simple, but yeah, the CLI should have given me more information.
You need to add the authorize information to mqtt connect method.Just like this.
var client=mqtt.connect("ws://192.168.1.1", {
username: "yourUsername",
password: "yourPassword"
}
Add the Authorization details for the client to connect
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://192.168.1.70', {
username: '<username>',
password: '<password>'
});
client.on('connect', function () {
client.subscribe('presence')
client.publish('presence', 'Hello mqtt')
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
client.end()
})

How to use redis PUBLISH/SUBSCRIBE with nodejs to notify clients when data values change?

I'm writing an event-driven publish/subscribe application with NodeJS and Redis. I need an example of how to notify web clients when the data values in Redis change.
OLD only use a reference
Dependencies
uses express, socket.io, node_redis and last but not least the sample code from media fire.
Install node.js+npm(as non root)
First you should(if you have not done this yet) install node.js+npm in 30 seconds (the right way because you should NOT run npm as root):
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl http://npmjs.org/install.sh | sh
Install dependencies
After you installed node+npm you should install dependencies by issuing:
npm install express
npm install socket.io
npm install hiredis redis # hiredis to use c binding for redis => FAST :)
Download sample
You can download complete sample from mediafire.
Unzip package
unzip pbsb.zip # can also do via graphical interface if you prefer.
What's inside zip
./app.js
const PORT = 3000;
const HOST = 'localhost';
var express = require('express');
var app = module.exports = express.createServer();
app.use(express.staticProvider(__dirname + '/public'));
const redis = require('redis');
const client = redis.createClient();
const io = require('socket.io');
if (!module.parent) {
app.listen(PORT, HOST);
console.log("Express server listening on port %d", app.address().port)
const socket = io.listen(app);
socket.on('connection', function(client) {
const subscribe = redis.createClient();
subscribe.subscribe('pubsub'); // listen to messages from channel pubsub
subscribe.on("message", function(channel, message) {
client.send(message);
});
client.on('message', function(msg) {
});
client.on('disconnect', function() {
subscribe.quit();
});
});
}
./public/index.html
<html>
<head>
<title>PubSub</title>
<script src="/socket.io/socket.io.js"></script>
<script src="/javascripts/jquery-1.4.3.min.js"></script>
</head>
<body>
<div id="content"></div>
<script>
$(document).ready(function() {
var socket = new io.Socket('localhost', {port: 3000, rememberTransport: false/*, transports: ['xhr-polling']*/});
var content = $('#content');
socket.on('connect', function() {
});
socket.on('message', function(message){
content.prepend(message + '<br />');
}) ;
socket.on('disconnect', function() {
console.log('disconnected');
content.html("<b>Disconnected!</b>");
});
socket.connect();
});
</script>
</body>
</html>
Start server
cd pbsb
node app.js
Start browser
Best if you start google chrome(because of websockets support, but not necessary). Visit http://localhost:3000 to see sample(in the beginning you don't see anything but PubSub as title).
But on publish to channel pubsub you should see a message. Below we publish "Hello world!" to the browser.
From ./redis-cli
publish pubsub "Hello world!"
here's a simplified example without as many dependencies.
You do still need to npm install hiredis redis
The node JavaScript:
var redis = require("redis"),
client = redis.createClient();
client.subscribe("pubsub");
client.on("message", function(channel, message){
console.log(channel + ": " + message);
});
...put that in a pubsub.js file and run node pubsub.js
in redis-cli:
redis> publish pubsub "Hello Wonky!"
(integer) 1
which should display: pubsub: Hello Wonky! in the terminal running node!
Congrats!
Additional 4/23/2013: I also want to make note that when a client subscribes to a pub/sub channel it goes into subscriber mode and is limited to subscriber commands. You'll just need to create additional instances of redis clients. client1 = redis.createClient(), client2 = redis.createClient() so one can be in subscriber mode and the other can issue regular DB commands.
Complete Redis Pub/Sub Example (Real-time Chat using Hapi.js & Socket.io)
We were trying to understand Redis Publish/Subscribe ("Pub/Sub") and all the existing examples were either outdated, too simple or had no tests.
So we wrote a Complete Real-time Chat using Hapi.js + Socket.io + Redis Pub/Sub Example with End-to-End Tests!
https://github.com/dwyl/hapi-socketio-redis-chat-example
The Pub/Sub component is only a few lines of node.js code:
https://github.com/dwyl/hapi-socketio-redis-chat-example/blob/master/lib/chat.js#L33-L40
Rather than pasting it here (without any context) we encourage you to checkout/try the example.
We built it using Hapi.js but the chat.js file is de-coupled from Hapi and can easily be used with a basic node.js http server or express (etc.)
Handle redis errors to stop nodejs from exiting. You can do this by writing;
subcribe.on("error", function(){
//Deal with error
})
I think you get the exception because you are using the same client which is subscribed to publish messages. Create a separate client for publishing messages and that could solve your problem.
Check out acani-node on GitHub, especially the file acani-node-server.js. If these links are broken, look for acani-chat-server among acani's GitHub public repositories.
If you want to get this working with socket.io 0.7 AND an external webserver you need to change (besides the staticProvider -> static issue):
a) provide the domain name instead of localhost (i.e. var socket = io.connect('http://my.domain.com:3000'); ) in the index.html
b) change HOST in app.js (i.e. const HOST = 'my.domain.com'; )
c) and add sockets in line 37 of app.js (i.e. 'socket.sockets.on('connection', function(client) { …' )
Update to the code:
staticProvider
now renamed to
static
see migration guide
according to #alex solution. if you have an error like this one as per #tyler mention:
node.js:134
throw e; // process.nextTick error, or 'error'
event on first tick ^ Error: Redis connection to 127.0.0.1:6379 failed - ECONNREFUSED, Connection refused at Socket.
then you need to install Redis first. check this out:
http://redis.io/download

Categories