mongo shell JavaScript equivalent to --sslCAFile - javascript

I'm using JavaScript to connect to multiple databases at once from the mongo shell. However, one of my databases is on Digital Ocean instead of AWS so I have an SSL certificate on my EC2 server.
In general, I use something similar to this to connect and use multiple servers on one file:
var iptDb = connect("###.###.###.###:27017/dbname);
iptDb.auth("username", cat("pw"));
In order to connect to my Digital Ocean database, I know that I need to add ?ssl=true, like below, but I don't know where I put the certificate information.
var iptDb = connect("###.###.###.###:27017/dbname?ssl=true");
iptDb.auth("username", cat("pw"));
mongo uses --sslCAFile in the shell to give the certificate path.
What would be the equivalent if I want to write it in my JavaScript
file?
More information on --sslCAFile at MongoDB Configure SSL

The mongo shell isn't intended to be a full driver replacement; some options (like --sslCAFile) can only be provided as command line parameters. As at MongoDB 3.2 there are other limitations of mongo shell scripts such as few I/O options (no input methods and limited output methods).
If your requirements are relatively straightforward and you don't want to install a driver, you could perhaps write a shell script (bash/zsh/...) to run multiple invocations of mongod with the expected connection parameters.
For any significant scripting I would recommend using one of the officially supported drivers which will include full support for SSL options. For example, see the Node.js driver SSL connection tutorial.

Related

Google Assistant Input to Python Output

the question is pretty straight forward. I would like to control a drone (Bitcraze Crazyflie), using a Google Home. The Input is: "Drone fly to x3 y4", processed as usual by Firebase etc. Resulting in the Google Assistant Output: "Flying to x3 y4", but also an Ouput in e.g. JSON format, to navigate the drone. Because the drone works with Python, this is the preferable Output language.
EDIT Added more Context
Currently I'm using an node server running this code:
'use strict';
// Import the Dialogflow module from the Actions on Google client library.
const {dialogflow} = require('actions-on-google');
// Import the firebase-functions package for deployment.
const functions = require('firebase-functions');
// Instantiate the Dialogflow client.
const app = dialogflow({debug: true});
// Handle the Dialogflow intent named 'fly'.
// The intent collects parameters named 'xaxis, yaxis'.
app.intent('fly', (conv, {xaxis,yaxis}) => {
const xAxis = xaxis;
const yAxis = yaxis;
// Respond with the user's coordinates and end the conversation.
conv.close('Roger that, flying to ' + xAxis + ", " + yAxis);
});
// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
Now I would like to get the const xAxis and yAxis and use them in a Python program. I've tried using
process.stdout.write(xAxis + yAxis);
Listening in Python with something like
out = sensor.stdout.read(1)
but the code will be run on the Google Server, so local port listening does not work.
Thanks for your help.
The best approach is having another machine on GCP rather than communicate with your home PC. You'll learn more and have an easier time, in the long run, building solutions. As I'm more familiar with AWS rather than GCP, I can't cite the network/security components you need to configure but the docs say you don't have to. So, in theory, it should be just about spinning up another compute machine with your Python code running on it.
Were you to decide on speaking to your home PC, you'll need to forward ports on your router. It is, currently, acting as a firewall for your LAN devices and doesn't permit outside machines initiating connections to your internal addresses. e.g. your GCP machine initiating a connection to your home PC. The other way around is permitted, by default. If you think about it, your router has one WAN IP address but your LAN can have multiple devices (multiple LAN IPs). If your GCP machine connects to your router WAN IP at port 8080, to which LAN IP should it connect? You have to help your router and explicitly tell it.
Once you have a networking solution in place, you can debug the connectivity itself (server can talk to client) by using netcat (nc/ncat, depending on Linux distro). Netcat is a versatile networking tool with which you can purely open connections (before you add in your program to the debugging stack) and assure the networking part of your solution is working as intended.
nc -v <destination_ip> <port>
Simple.
This should get you to where you want to be. A working connection between your GCP drone controller and the Python processor machine.
Bonus - If you want a quick way to have your machine (PC or otherwise) listen on a port, you can use Python's built-in HTTP file server module with
python -m http.server 8080
This will serve files from the directory you ran this command. So, keep that in mind if you're open to the world.
Or, a simple "echo server", using netcat.
nc -v -l 8080
Lastly, for a solid Python HTTP API framework, I highly recommend FastAPI. It allows quickly writing a HTTP API server with, for example, a POST method that your GCP drone controller can call. It has the great bonus of generating both interactive OpenAPI docs, example, for your code and, using 3rd party tools from Swagger (that you can see in the example linked), generate server/client/testing "boiler plate" code. Did I also mention their docs are great?

How to give the server port to client

I have a node server with several node projects. I use nginx to get them all responding on port 80. Now, this works for the initial http request. For the websockets, I need to use the direct server port. To keep everything alive while developing I would like to try this, projects will have a dev and live version. Once de dev is stable, I will copy it to the live folder. The live folder is runned by a systemctl script where I define a difrent port to the live version so I can dev without taking the live down. The problem I encounter now is, how can I get the running server port in my client side Javascript so that the dev page connect to the dev port and visa versa?
currently I'm only using express, socket.io and mysql. I have no further npm packages installed. I searched allot but there is not to mush I can find. I found how to connect the socket to the page url but I cannot use that because that URL will always be on port 80. Further I found allot of huge packages that has no use for me since the original page is just static, the dynamics all run over websockets.
Is there any way to parse the port number in the clients .js file like I could do fairly easy in php? And if so, what would be the most efficient way. I could let javascript check if the page uses the live or dev URL but I would prefer not to hardcode my dev URL into JS where it is for everyone to see.
run a third node.js socket.io server program, all your clients connect to this server first.
In this simple node.js program, determine the type of clients by any means. e.g. different user id for dev/production users
send the server url and port to your client according to its type (dev or production)
you may also use this technique to separate your users to different production servers.

Creating a NodeJS based web server to take advantage of HTTP2 on windows platform

I am using windows 2012 server and want to host some static HTML/CSS/JS/image files on a nodejs based web server. I do not want to use IIS as I want to take advantages of HTTP2 & want to push files from server to client. I looked at Using node.js as a simple web server which talks about how to create a node based webserver. Another option is to use http-server node package.
My question is:
These solutions are over two year old. Do we have a better option available now?
Does any of these two options supports HTTP2?
I would prefer using a existing node module rather then reinventing the wheel.
You could try NGINX, it can support HTTP/2. http://nginx.org/en/docs/windows.html
Run your node applications by using default node, nodemon, pm2...
Then use NGINX as a static web server and you can reverse proxy your node apps.
If you want to use Node then this article seems to cover the basics: https://webapplog.com/http2-server-push-node-express/ and it seems the node-spdy module is the best option (it includes support for HTTP/2 despite the name). There is a node-http2 module but it seems much less well maintained and doesn't support Express (the most popular HTTP framework for Node).
However, as discussed in the comments, while not the question you asked, I recommend running a traditional web server (e.g. Apache, Nginx or IIS) in front of NodeJS or any other traditionally back end server. While NodeJS is very flexible and most (if not all) of the functionality of a webserver can be added to it, a traditional web server comes out of the box with a lot of functionality and requires just configuration rather than programming and/or pulling in multiple other modules to set it up properly.
For just serving static files Node seems the wrong solution to me so, for the rest of my answer I'll discuss not not using Node directly for the reasons given above but instead using a front end webserver.
I don't know IIS too well but from a quick Google it seems HTTP/2 was only introduced in IIS 10 and, as far as I know, even IIS 10 doesn't support Push except through API calls so I agree with your decision not to use that for now.
Nginx could be installed instead of IIS, as suggested, and while it supports HTTP/2 it doesn't yet support HTTP/2 (though Cloudflare have added it and run on Nginx so imagine it won't be long coming).
Apache fully supports HTTP/2 including server push. Packaged windows versions of Apache can be downloaded from Apache Lounge so is probably the easiest way of supporting HTTP/2 push on Windows Server and would be my recommendation for the scenario you've given.
While I mostly use Apache on Linux boxes I've a number of servers on Windows and have quite happily been running Apache on that as a Service (so it automatically restarts on server reboot) with no issues so not sure what "bad experience" you had previously but it really is quite stable to me.
To set up Apache on a Windows Server use the following steps:
Download the last version from Apache Lounge.
Unzip the files and save them to C:\ (or C:\Program Files\ if you prefer but update all the config to change the default C:\apache24 to C:\Program Files\)
Edit the conf\httpd.conf file to check ServerRoot, DocumentRoot and any Directory values are set to where you want it (C:\Apache24 by default).
Run a DOS->Command Prompt as Administrator
In the Administrator CD to the Apache location and the bin director.
Run httpd.exe and deal with any error messages (note port 80 must be free so stop anything else running on that report).
Check you get the default "It works!" message on http://localhost/
Install Apache as a service by killing the httpd.exe process and instead running httpd.exe -install.
Start the Apache24 service and again verify you get the "It works!" message on http://localhost/
To add HTTP/2 and HTTPS (necessary for HTTP/2 on all browsers), uncomment the following lines from httpd.conf:
LoadModule http2_module modules/mod_http2.so
...
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
...
LoadModule ssl_module modules/mod_ssl.so
...
Include conf/extra/httpd-ssl.conf
Install a cert and key to conf/server.crt and conf/server.key - note Apache 2.4 expects the cert file to include the cert plus any intermediary certs in X509 Base 64 DER format so should look something like this when opened in a text editor:
-----BEGIN CERTIFICATE-----
MII...etc.
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
MII...etc.
-----END CERTIFICATE-----
Where the first cert is the server cert and the 2nd and subsequent certs are the intermediaries.
You should make sure you're running good HTTPS config (the defaults in Apache are very poor), but the defaults will do for now. I've a blog post on that here.
Restart Apache in the service menu and check you can access https://localhost (ignoring any cert error assuming your cert does not cover localhost).
To add HTTP/2 to Apache
Edit the conf/extra/httpd-ssl.conf file to add the following near the top (e.g. after the Listen 443 line):
Protocols h2 http/1.1
Restart Apache in the service menu and check you can access https://localhost (ignoring any cert error assuming your cert does not cover localhost) and you should see h2 as the protocol in the developer tools of your web browser.
To use HTTP/2 push in Apache add the following to push a style sheet:
Header add Link "</path/to/css/styles.css>;rel=preload;as=style" env=!cssloaded
And you should see it pushed to your page in developer tools. Again, I've a blog post on that if you want more information on this.
If you do want to use Node for some (or all) of your calls you can uncomment the following line from conf/httpd.conf:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
And then add the following config:
ProxyPass /nodecontent http://127.0.0.1:8000/
Which will send any of those requests to node service running on port 8000. Restart to pick up this config.
If your node service adds any HTTP headers like this:
link:</path/to/style/styles.css>;rel=preload;as=style
Then Apache should pick them up and push them too. For example if using Express you can use the following to set the headers:
app.get('/test/', function (req, res) {
res.header('link','</path/to/style.css>;rel=preload;as=style');
res.send('This is a test page which also uses Apache to push a CSS file!\n');
});
Finally, while on the subject of HTTP/2 push this article includes a lot of interesting food for thought: https://jakearchibald.com/2017/h2-push-tougher-than-i-thought/
I know this is a fairly old question, but I thought I would give an answer for those that come here looking for info.
Node now has a native http2 module and there are some examples on the web that show exactly how to implement a static web server.
NOTE: At the time of this answer Node 9.6.1 is current and the native module is still experimental
Example https://dexecure.com/blog/how-to-create-http2-static-file-server-nodejs-with-examples/
NOTE: I have no affiliation to the author of the example

Testing a Single Page Application while simulating the backend

I have a single page application developed using javascript, react and redux that communicates with the backend via web socket, rest and json.
I wish to explore the possibility of working independently of the backend, both as I develop and in terms of treating the front end as a subsystem and performing subsystem tests on it.
Is this a good approach? What tools would you recommend? For example, I have used selenium previously with python. Would it be a sensible option for me to write my own simple test environment using a selenium api where tests are configured in their setup to respond to rest requests from the front end in the appropriate manner. Maybe there are better approaches.
I think npm provide http-server
You can run server in your project dir and it should be server on localhost.
But if you are using Linux or Mac, the easiest way will be:
$ cd you_app_folder
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
As python is built in on mac and Linux it is easiest way as I said.

Install node.js at Shared Dreamhost

Im trying to install node.js to shared dreamhost. I have followed the directions for Dan dean here http://dandean.com/nodejs-on-dreamhost-shared-server/
but when I go test,
$ node --version
Then it says:
-bash: node: command not found
I follow the step by step, this is the correct way to install the node in the shared server?
Dreamhost has supported Node.js on shared host recently.
I put the following source code in app.js and it works.
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end("Hello World!\n");
});
server.listen(3000);
But it seems to be unstable when I try to restart it.
Check https://help.dreamhost.com/hc/en-us/articles/217185397-Node-js-overview for details.
You may want to look into nvm when installing Node somewhere else than your local environment.
Check out my blog post on nvm:
http://mycodesmells.com/post/node-version-management/
Sometimes, installing node.js on certain linux instances creates the command nodejs instead of node. Try nodejs --version. If you want to be able to use node as the command, simply create a simlink to /usr/local/bin/nodejs for /usr/local/bin/node.
Currently DreamHost says “Node.js can be installed onto Shared, VPS, and Dedicated Servers.” However, for shared servers, currently Node is only supported using Passenger, and the version of Passenger “running on DreamHost servers does not currently function with Node.js versions 14+” as of March 1, 2022. They have not shared a target date for allowing newer versions of Node.
For more instructions, and up-to-date compatibility information, see their support documentation on Node.js at Dreamhost.
It may not have been the case at the time this question was posted, but Dreamhost does not allow compiling or running nodejs on shared hosting.
Can Node.js be used on shared servers?
No. DreamHost does not support Node.js on shared web servers, as the security setup on DreamHost shared servers is incompatible with compiling or running Node.js.
Furthermore, Dreamhost claims they will ban users who attempt to do so:
If you try to compile Node.js on one of the shared web servers, your user will automatically be banned through grsec (taking down all the PHP websites that run under that user) and the server will have to be rebooted before your user can be unbanned. If you do it one more time, you will be forced to move to a VPS.
I've had Dreamhost shared hosting for nearly 10 years, but they don't allow long-running processes. This also prevents common tools like composer from running if they take more than a few seconds. I recently moved to a VPS because of this.
https://help.dreamhost.com/hc/en-us/articles/217185397-Node-js-overview

Categories