Finding MongoDB details after hosting on Ubuntu - javascript

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

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.

Error: The server does not support SSL connections | PostgreSQL + Knex

I have my back-end at Heroku. The connection file looks like this:
const knex = require("knex")({
client: "pg",
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
port: 5432,
ssl: {
rejectUnauthorized: false,
},
},
});
The listening:
server.listen(process.env.PORT || 5000);
At insomnia:
Insomnia route + body + error message
I'm able to test my application through the Heroku deploy without problems, but locally, i cannot. My env variables are configured, everything works fine, except the localhost requests.
Any ideias?
Just found out the problem guys. My dotenv wasn't working properly. Although my files were in the correct place, exactly how the documentation asks for, my .env wasn't being recognized (?). I found out this by console logging one of my environment variables. Found out to be undefined, so I pointed out the location of the .env file (default location, above src folder...), and everything started working as it should.

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.

Node.js sensitive information in javascript file?

Is it safe to contain sensitive information such as database connection details on a JavaScript file running on a Node.js server ?
For instance:
var mysql = require('db-mysql');
new mysql.Database({
hostname: 'localhost',
user: 'user',
password: 'password',
database: 'test'
}).on('error', function(error) {
console.log('ERROR: ' + error);
}).on('ready', function(server) {
console.log('Connected to ' + server.hostname + ' (' + server.version + ')');
}).connect();
Since JavaScript file is a client-side file, is this information can't be seen through the client on a typical browser using the developer tool ?
Since you're executing the script server-side, this same code is not viewable on the client-side. Think of it along the same lines as a PHP script or similar. However as already pointed out, if you place your script inside a publicly accessible directory, then people could see the code.
A couple of alternatives to placing your credentials directly inside your script could be to move your credentials securely (e.g. with appropriate file/user permissions) to a file in some other directory that your script reads from or get your credentials from the environment like:
# DB_USER=foo DB_PASS=bar node myscript.js
Then inside your script:
new mysql.Database({
hostname: 'localhost',
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: 'test'
// ...
Yes, it's safe to do that. Client will have only access to what you will send to him from your application. This is not client-side file, it's node.js and your application server-side file. Remember to NOT include this file with other JS files like jquery etc which you will send to your clients.
Although it is safe, but what If directory browsing is enabled on server ?

Meteor.js + external Mongo. Meteor cannot login into Mongo

I have a meteor.js app + mongo db (2.6).
I've created a user in mongo like this:
use meteor
db.createUser(
{
user: "meteor",
pwd: "password",
roles:
[
{
role: "userAdmin",
db: "meteor"
}
]
}
)
here is my mongodb.conf:
dbpath=/data/db
logpath=/var/log/mongodb/mongodb.log
logappend=true
port = 27017
when i set auth = true then my meteor app cannot connect to mongo anymore. It says
Exception in callback of async function: MongoError: auth failed
Same error when i try to connect with RoboMongo when auth is true. When auth is false i can connect with RoboMongo. So it's not about Firewall or something.
I don't understand, what I can do to switch on authorization in Mongo, so that it would let me login. Please help.
It depends on how you start Meteor. You need to tell it which Mongo instance to use and also provide proper credentials like this:
export MONGO_URL=mongodb://<username>:<password>#<host>:<port>/<db>
That's the way I've been doing it with my Meteor apps and a dedicated MongoDB and it works nicely. But if RoboMongo is not able to connect, there may be something wrong with the Mongo configuration. You can use this command to enable authentication in your config:
mongod --auth --config /path/to/mongodb.conf
I assume you have an admin user already which was used to create the meteor user, you will run into issues if you start Mongo without authentication, then add a meteor user anonymously and restart with auth=true.

Categories

Resources