Unable to run node API on Ubuntu (AWS) - node.js

I have deployed node API on Ubuntu server (AWS) but when I run my app it throw below error given in screen shot. due to I am not able to make API requst.
I have trid to kill port and execute it again but it gives e same error
my node app is running on 8081 on local machine
var connection = mongoose
.connect(url, option)
.then(result => {
app.listen(PORT);
console.log("Running RestHub on port " + PORT)
})
.catch(err => {
console.log(err);
});
after uploading code in Ubuntu sever it does not work
I am struggling to fix this issue since yesterday but could not fix yet.
Please your help would be highly appreciated
Thanks you

Related

redis is working in local but not working in server

I have a redis server which is working fine in my local but in ubuntu server is not working can someone gives the comment for installing redis in server
it is not working even with docker it is working only while i am running in local
const redis=require('redis');
var redisClient:any;
(async () => {
try {
redisClient = redis.createClient({ socket: { port: 6379 } });
await redisClient.connect();
// const redisClient = redis.createClient({
// port:"6379",
// host:'redis-service'
// });
redisClient.on('connect',()=>{
console.log('server connected to redis')
})
redisClient.on('ready',()=>{
console.log('Client Connect to redis and ready to use')
})
redisClient.on('error',(err:any)=>{
console.log(err)
})
redisClient.on('end',()=>{
console.log('Server disconnected from redis')
})
process.on('SIGINT',()=>{
redisClient.quit()
})
console.log('connected');
} catch (err) {
console.error(err)
}
})()
export{
redisClient
};
Make sure that Redis is not already running on the server. You can check this by running the command ps aux | grep redis. If Redis is running, you should see a line with the redis-server process.
Confirm that Redis is properly installed on your server by running redis-cli ping. If Redis is installed and running, it should return "PONG"
Check the Redis configuration file (redis.conf) and ensure that the IP and port settings match the settings on your local machine.
Make sure that the Redis server has the necessary permissions to access its data directory and that the correct ownership and permissions are set on its files.
Verify that the firewall rules on the server allow incoming connections on the Redis port (usually 6379)
Check for any compatibility issues between the version of Redis that you're running on your local machine and the version of Redis that's running on the Ubuntu server. If there's a version mismatch, it may be necessary to upgrade or downgrade one of the installations.
Try to run the Redis server with verbose output by running the command redis-server -v, this can give you some more information about the errors that are causing the server to fail.
Look at Redis log files, usually located in /var/log/redis for further clues about the cause of the problem.

Cloud9: "Invalid host header" on React & nodejs app

Im developping a Reactjs & nodejs webapp on AWS's cloud9.
My problem is that i struggle to make a HTTP request from Reactjs frontend to my express backend server.
Both back and front run well separately.
React run on port 8080 and express on port 8081.
Here is the frontend request:
async function getAccessToken(){
await axios.get('/api/token',{
keepAlive: true}).then(function(response){
response.json().access_token;
}).catch(function(error){
console.log('Error dans App.js: ', error);
});
}
Heres the server :
const secureServer = https.createServer({passphrase:process.env.PASSPHRASE, cert: certfile, key: keyfile,}, app);
secureServer.listen(process.env.PORT_SERVER, console.log('Server started on port', process.env.PORT_SERVER));
When i start both and access the application preview, i get an "Invalid host header".
So i searched here, and i dont want to mess with react-scripts webpack.config.js.
I tried to set a "proxy": http://0.0.0.0:8081 in react package.json, to no avail.
Im at a loss here and hope someone can help.
Thanks in advance.

Nodemon server won't start

I was trying to create a simple room based webchat app using socket.io. But I've been facing issues with serving the node server file with node and nodemon. I've tried different ports i.e., 8000, 8080, 80 and I've also tried reinstalling node and nodemon both locally and globally but nothing seems to work. I guess the code is alright because I was following a tutorial (that didn't include any link to the source code though). I'm on windows so I've also used used the command
Set-ExecutionPolicy Unrestricted
Here are the screenshots of Windows power shell and command prompt
PS: I am a newbie, so please consider that I might have missed something basic.
const io = require('socket.io')(8000)
const users = {};
io.on('connection', socket =>{
socket.on('new-user-joined',name =>{
users[socket.id] = name;
socket.broadcast.emit('user-joined', name);
});
socket.on('send', message =>{
socket.broadcast.emit('receive', {message: message,name: user[socket.id]});
});
})

connecting node js to postgresql on heroku

I have deployed my app on heroku and was able to make a connection to the database from pg:psql in my terminal by copying and pasting the code provided by heroku. Now i want to directly connect my node app to postgres on heroku but no success. I followed the instruction on heroku on how to connect nodejs to postgres here.. https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js but i can't seem to get it to work. I simply installed pg module and pasted the block of code provided by heroku inside my get request in my server.js file.
app.use(express.static('public'));
app.use(bodyParser.json());
app.get('/food', function(req, res) {
pg.defaults.ssl = true;
pg.connect(process.env.DATABASE_URL, function(err, client) {
if (err) throw err;
console.log('Connected to postgres! Getting schemas...');
client.query('SELECT table_schema,table_name FROM information_schema.tables;').on('row', function(row) {
console.log(JSON.stringify(row));
});
});
});
http.listen(process.env.PORT || 3000, function(){
console.log('listening to port 3000!');
});
the error i get from heroku logs is..
/appserver.js:53
if (err) throw err;
Error: connect ECONNREFUSED 127.0.0.0.1:5432
after searching for that error message, i get some people suggesting on fixing or changing the DATABASE_URL. How and where do i change it? I'm lost. I would really appreciated if i can get some help to fix it.
The pg connection is using the database url in your environment:
pg.connect(process.env.DATABASE_URL, function(err, client) {}
Because the DATABASE_URL env variable is not set, it defaults to localhost 127.0.0.0.1:5432. You can set DATABASE_URL in your bashrc or bash_profile. You can name it more specifically PROJECTNAME_DATABASE_URL if you have multiple apps. On Heroku, you can set it like this:
$ heroku config:set DATABASE_URL=postgres://user:password#host:port/database

Node JS Net.Connect ECONNREFUSED

I created a very simple Node.JS script which is suppose to connect to a server on port 3200. The code works great on Windows 8.1 x64, but when I moved it to a Ubuntu Server machine I am getting a ECONNREFUSED.
I tried to connect via IP Address and the Domain Name, but neither work. It always gives me ECONNREFUSED.
When I SSH into the Ubuntu Server I can do a telnet to the server/port and it works fine. Also pinging the server works. Just Node.JS doesnt connect.
Server:
Ubuntu Server 12.04.3 LTS (precise)
Here is the code:
var net = require('net');
var socket = net.connect({port: 3200, host: '172.19.16.203'}, function() {
// Connection Successful
console.log('Socket connected...');
// Data Received from Server
socket.on('data', function(d) {
console.log('data');
});
socket.on('end', function() {
});
socket.on('error', function(e) {
});
});
I found the solution. I was running my script by the following command:
node test.js
I guess the node is an old version. I upgraded the OS to the latest version and then did:
apt-get install nodejs
I run the script like following:
nodejs test.js
And it works!

Resources