Nodejs Express API with NATS? - node.js

I am trying to build a REST API for my frontend app using nodejs + express + nats.
I have a nats-server running in my terminal.
This is my test code:
var express = require("express");
var app = express();
const NATS = require('nats');
const nc = NATS.connect();
nc.on('connect', () => {
console.log('connected');
})
nc.publish('foo', 'Hello World!');
app.get('/', (req,res) => {
nc.subscribe('foo', function (msg) {
res.send(msg)
});
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});
After running test code, localhost:3000 cannot be reached.
I found a similar project on github: https://github.com/georgehaidar/poc-express-nats/blob/master/api.js.
I cant seem to find my error.
Can anyone plz help me figure out what im doin wrong?
Thank you in advance.

It looks like you've not provided the nats.connect() method with a hostname like the original author does:
const nats = require('nats').connect({'servers': ['nats://nats:4222']})
Nats Connection documentation:
https://docs.nats.io/developing-with-nats/connecting/specific_server

Related

Express Doesn't Display Json data

Hello I am new in node js and i am Facing the error.
fb-downloader package is working fine with console.log()
But i want to display results on webpage. But the Express displays only {}
Please Help me.
const getFBInfo = require("fb-downloader");
const express = require("express");
const app = express();
const port = 3016;
app.get("/",function(req, res){
data = getFBInfo("https://www.facebook.com/watch?v=272591278381388");
res.json(data);
});
app.listen(port, function() {
console.log(`Server is listening on port http://localhost:${port}!`)
});
Sorry for Bad English
getFBInfo is an asynchronous operation, therefore use async and await:
app.get("/",async function(req, res){
var data = await getFBInfo("https://www.facebook.com/watch?v=272591278381388");
res.json(data);
});

Debian server, Node js Port 3000 listening But cant access via browser

On my debian server, I installed node and then started node server on port 3000. The server is running, but it isn't visible from the browser
Now when I try to get it running via my domain or via my ip(for example xx.xxx.xx.xx:3000) or my domain (my-domain.com:3000) in both cases it doesn't work. I think I don't quite get the concept and I tried to search for a billion different things, but I can't find the solution to my problem. Could someone tell me, if I need to setup something else, too?
My server js code is
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
server.listen(3000, () => {
console.log('listening on *:3000');
});
io.on('connection', function (socket) {
socket.on( 'new_message', function( data ) {
io.sockets.emit( 'new_message', {
message: data.message,
date: data.date,
msgcount: data.msgcount
});
});
});
Error i got
You need to listen for GET requests in order to respond to them.
Try adding something like:
app.get('/', function (req, res) {
res.send('GET request test.')
})
In your case make sure you add the route before passing the app to the http.createServer() method, or otherwise just use something like app.listen(3000).
More info in the docs: https://expressjs.com/en/guide/routing.html
why are you using express and http both packages.
you can run server by either of them.
and then add a get route for it.
import { createServer } from "http";
import { Server } from "socket.io";
const httpServer = createServer();
const io = new Server(httpServer, {
// ...
});
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
I hope this will work!

Why there is always request timeout error in heroku

I just want to deploy a simple node js application on heroku but it always throws me a request timeout error, I don't know why? Actually, first I tried with my main project but it didn't work then I try with some basic node js code, and after deploying I literally got shocked because it is also not working. Is this some problem related to the WebSockets library or I am doing wrong (Please let me know), I am tired of searching for a solution to this problem.
server.js
const Socket = require("websocket").server
const http = require("http")
const server = http.createServer((req, res) => {})
server.listen(process.env.PORT || 3000, () => {
console.log("Listening on port 3000...")
})
const webSocket = new Socket({ httpServer: server })
webSocket.on('request', (req) => {
const connection = req.accept()
console.log("open")
connection.on('message', (message) => {
const data = JSON.parse(message.utf8Data)
if(data.type == "message"){
console.log(data.mess);
connection.send(JSON.stringify(data))
}
})
})
Please help me to get out of this.

I want to check the server.When I tried to run the code in console using:Node server.js, it didn't run the code. Any ideas why?

I want to check the server.When I tried to run the code in console using:Node server.js, it didn't run the code. Any ideas why?
This is my code where I tried to check the server
const express = require("express");
const app = express();
app.get("/", function(request, response){
console.log(request);
})
app.listen(3000, function () {
console.log("Server started on port 3000");
});
and here I try to run the code in console, and it simply doesn't do anything: it shows me the location and that's all
Helens-MacBook-Pro:my-express-server helenmyrlen$ node server.js
Helens-MacBook-Pro:my-express-server helenmyrlen$
I am also new to NodeJS so I can't find where you are doing are but you can try with the below script. It works.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Run the app with the following command:
$ node app.js
Then, load http://localhost:3000/ in a browser to see the output.

appmetrics(https://github.com/RuntimeTools/appmetrics/issues) request and http event not working

Hi Please see below sample code.
const express = require('express')
const app = express()
var port = process.env.PORT || 3000;
var appmetrics = require('appmetrics');
var monitor = appmetrics.monitor();
appmetrics.enable('http');
appmetrics.enable('request');
monitor.on('request', function (request) {
console.log('request', request);
});
monitor.on('http', function (http) {
console.log('http', http);
});
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(port, function () {
console.log('Example app listening on port 3000!')
})
'
Whenever I fire localhost:3000 from browser. I get 'Hello World!' reply but logs doesn't show any request or http event. Can somebody help me to point out issue.
I am using latest appmetrics version.
Thanks in Advance.
Have you tried putting line 4 (the appmetrics require line) at the top of the code? If it works, please close the issue you raised https://github.com/RuntimeTools/appmetrics/issues/452

Resources