Connecting to postgresql database remotely - javascript

how to Connect postgresql using bookshelf.js from a remote system instead of using "host: localhost"

Use the hostname or IP address of the host, instead of 'localhost', like:
host: "dbserver.example.com"
or
host: "203.0.113.1"
Note: if the database is not using the default port (5432) you should also specify it. For example for port 5050:
host: "dbserver.example.com:5050"
or
host: "203.0.113.1:5050"

Related

Requests to nodejs server deployed on Azure App Service timing out when trying to access Azure Postgres flexible database

I have a node.js express server deployed on Azure App Services, which connects to an Azure flexible postgresql database to serve requests. When running the server locally the postgres database can access the database fine, but when the server is deployed to an azure app service, all requests time out:
The server uses a pool to make requests, this is what my server config file looks like:
const {Pool} = require('pg');
require('dotenv').config();
const config = {
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DB_NAME,
port: process.env.PORT,
ssl: true,
max: 50,
idleTimeoutMillis: 10000,
allowExitOnIdle: true,
}
module.exports = new Pool(config);
So far I've tried:
Allowing all IP addresses to access the database
Allowing all services within Azure to access the database
Giving the server's App service contributor permissions to the database.
But none of these solutions have prevented requests from timing out, so any help would be greatly appreciated.
Thank you
I have thankfully found the solution to this problem!
I suspect that the Azure App Service overrides the PORT env variable to 8080, and so the port variable used to connect to the database ends up being wrong when a connection is attempted.
I've renamed the .env variable to something else and it is now working correctly.

How to connect from MQTT javascript client to Mosquitto Server?

error log in console browser : "WebSocket connection to 'ws://127.0.0.1:1883/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET"
my code .js to connect mosquitto server:
var options = {
clientId: 'web-client',
connectTimeout: 5000,
hostname: '127.0.0.1',
port: 1883,
path: '/mqtt'
};
var client = mqtt.connect(options);
use library mqtt-2.9.0.js
use mosquitto v1.5.4 windows10
=========================================
By default Mosquitto listens on port 1883 and accepts connections using native MQTT
If you want to connect with MQTT over Websockets you need to configure Mosquitto to listen on a different port and specify to use the websockets transport.
You can add the following to your mosquitto.conf:
listener 8883
protocol websockets
This will cause mosquitto to listen on port 8883 for MQTT over Websockets conections.
You can then modify your code as follows:
var options = {
clientId: 'web-client',
connectTimeout: 5000,
hostname: '127.0.0.1',
port: 8883,
path: '/mqtt'
};
var client = mqtt.connect(options);
It's also worth pointing out that your clientId needs to be unique for EVERY client that connects, so you will need to make it dynamic if you are going to load the page more than once at a time.
While 1883 is the usual port for vanilla MQTT connection - the usual default for websockets is port 8883. Have you tried port 8883?
Durr edited my typo 8888 to 8883

Deploy PeerJS server on Heroku

I have a problem with PeerJS server. I used "Deploy to Heroku" button from here:
https://github.com/peers/peerjs-server
I have no idea how can I connect with deployed cloud.
I can't find clear documentatnion about PeerJS Server.
I don't know what is the host, port, and path for my app.
var peer = new Peer('someid', {host: 'localhost', port: 9000, path: '/myapp'});
Please advice.
This how it worked for me:
var peer = new Peer('someid', {
secure: true,
host: 'your-app-name.herokuapp.com',
port: 443,
});
Your host is simply the web address to your Heroku app. For instance, if your Heroku app is named peerjsapp, then host would be 'peerjsapp.herokuapp.com'. You can find the name of your app on your Heroku dashboard. The port is usually 9000, but can be 443 if you're using HTTPS (make sure to also pass in secure:true if you're using HTTPS). You don't need to include the path unless you've changed it; if you're running the default server config, leaving out the path on your client will automatically connect. Finally, since you're hosting your own server, you don't need an ID.
• This is how I think you should do it:
const myPeer = new Peer(undefined, {
secure: true,
host: '0.peerjs.com',
port: '443'
})
• EXPLANATION:
After deploying your app to Heroku, typed 'peerjs' into the console to search for the peerjs object, from which you can navigate and find the key-value pair of
CLOUD_HOST: "0.peerjs.com"
CLOUD_PORT: "443"
The next step is just to match your own host and port with these values.
This is how I do it Console Screenshot
• NOTE:
For the secure: true part I have tried and the app works both with and without it. So it's on you to choose to include it or not. I have also found out on https://peerjs.com/docs.html this same information, check it out if you want more detailed documentation.

Finding MongoDB details after hosting on Ubuntu

I'm completely new to setting up servers, MongoDB, and still a little new to Javascript.
I'm trying to upload a Deployd server onto an online server. There is limited information on this, so at the moment, I set up a simple AWS Ubuntu server by doing the following tutorials:
http://zenborgium.blogspot.com/2012/12/how-to-setup-deployd-on-ubuntu-server.html
http://terraltech.com/how-to-setup-deployd-on-ubuntu-server/
However, I'm stuck at creating the production.js. There's a guide on it here. I'm specifically stuck at this line of code:
var server = deployd({
port: process.env.PORT || 5000,
env: 'production',
db: {
host: 'my.production.mongo.host',
port: 27105,
name: 'my-db',
credentials: {
username: 'username',
password: 'password'
}
}
});
server.listen();
Where do I find the host, port, name, and credentials that I should use from MongoDB? The tutorials say I need to use my own data, but I don't know where or how to find them.
First of all, you have to have MongoDB installed. I haven't used Deployd myself, but I will give you some information regarding the config file.
If you are running Mongo on the same ubuntu server as your application, you can use localhost to connect.
Default Mongo install runs on port 27017, in other words localhost:27017.
The 'name' parameter is just a name you give your database. So here you can put whatever you want, ex my-db.
With a clean Mongo install, you don't need any credentials. You have to set that up yourself if you want. I suppose leaving them out of the config file is ok, if not needed.
Your config file should therefor look something like this:
var server = deployd({
port: process.env.PORT || 5000,
env: 'production',
db: {
host: 'localhost',
port: 27017,
name: 'my-db'
}
});
update
I had a quick look at the tutorial you linked to. In one of the tutorials they created a user for mongodb. If you followed this step, you need to put that login information into you connect-object under credentials.
update 2
To get information about your mongodb install, check this SO post

how do i connect an existing database to sequelize.js?

I have a database I generated through postgres. How do I connect it to sequelize for node.js?
I know the basic syntax for sequelize connections, but how do I connect these together?
In node.js after installing the sequelize and pg packages through npm you can do this:
var sequelize = new Sequelize('your_database_name', 'user', 'password', {
host: "localhost", //your server
port: 12345 //server port
dialect: 'postgres'
});
Note, in order to use Sequelize with Postgres you'll_need the postgres package. The Sequelize homepage contains instructions on changes you might need to make in order to use them together.

Categories

Resources