Acessing MongoDB on the Client-Side using JavaScript - javascript

I know this is completely unsafe to do because the user would have control over the credentials. But in application I'm building I don't really care it's completely insecure.
Is there a way to open a connection on a MongoDb using client-side JavaScript ?
The JavaScript driver on Mongo's website is for use on NodeJS only.
Thanks,
Fred_

Take a look at the mongodb http interfaces list. Start a mongodb, start the sever, request/modify a data from that server using ajax.

Related

Fetching Data From MySQL as Json in ReactJs

The bounty expires in 7 days. Answers to this question are eligible for a +50 reputation bounty.
Om Nigam wants to draw more attention to this question.
How do i fetch data from my MySql database as json and then use it in react js...? actually i'm new to React and MySql so i just don't know much about it.. Please help
React is not allowed to connect to MySql directly. We need NodeJs to start a back-end server which supporting HTTP API.
You can watch this video, and try to create your demo.
React Node.js MySQL CRUD Tutorial for Beginners
I agree with YinPeng.Wei's idea for setting up a backend server to retrieve data from database. The majority programming language for backend development have packages or libraries to connect with Database.
A similar question was asked years ago about How to connect to SQL Server database from JavaScript in the browser?. The answer is you could do that but it is high discouraged.
The general idea is to create a simple backend server which responds to your React frontend requests. Backend retrieves data from MySQL via sql query, then serialize searched data into JSON format and gives back to your frontend.
Not sure which type of programming language would you choose for backend development. I would do either one of Python, C#, Java for a quick demo.
Typically, you are using React to connect to another app (backend) via an API e.g. REST, THAT backend is the one who connect to the database and passes the data to your React app.
Your react app doesn't even know there is a database, all he knows is the backend that's feeding him. without him he's just a pretty looking dead app.
now from your backend, you have two ways, using languages built-in driver to connect to your database directly (hand writing SQL statements), Or you may use an ORM, just google "js orm" and you will find many.
As a start, you need to learn more about creating a simple REST API with whatever language you choose, and then simply use fetch("http://example.com/whatever").then(res=> JSON.parse(res)).then(res=> console.log(res))
the code above should show you whatever you see on the screen when you actually visit the URL inside fetch() from the browser, just text.
REST itself is just a way to use HTTP that people like.
the browser itself is just another client (like your React app), if he can do it so does your app.
first, you have to solve the console error of key of react, give the key wherever you

How does one go use MongoDB with native JS?

I have been searching but sadly haven't found any resources for using MongoDB with native JS. Every resource I find seems to be NodeJS.
I want to use MongoDB on vanilla JS, so to speak.
See How to connect MongoDB with VanillaJS?
and also link from top comment Connecting MongoDB to the front-end?
You see everyone use node.js because by design, mongodb is meant to communicate with the server only and the server decides how to handle the data. By using nativejs directly (assuming it was possible without using cloud APIs) is a huge security risk. NativeJS advertises itself as "running inside your browser", I wouldn't want my backend code to be exposed to the user.
TL;DR you should use cloud integrations of mongoDB (mongoDB cloud etc.) or use node.js like everyone else.
TL; DR: You can't
MongoDB is not supposed to expose data directly to front-end (client side). NodeJS (in this case; MongoDB can be used with a variety of other server-side engines) is a server-side JavaScript engine that acts as a bridge between the client and the database.
However, you can have a look at MongoDB APIs
The better way, however, would be to have a server between your database and the web interface, that queries or inserts data from your MongoDB database. If your database has to be client-sided though, have a look at firebase.
If you only want to store data on the client, you could simply use LocalStorage API
If not, you either need to render all the data needed in your page content, or query it via js Fetch API
Even the native mongosh is a NodeJS environment, see MongoDB Shell:
The MongoDB Shell, mongosh, is a fully functional JavaScript and Node.js 16.x REPL environment for interacting with MongoDB deployments.
The legacy mongo shell is also a JavaScript shell, seems to be based on SpiderMonkey, see https://www.mongodb.com/community/forums/t/why-change-v8-to-spidermonkey-in-mongodb-js-engine/99871

Safe Javascript/Sql connection

im trying to create a simple website with HTML/CSS and Javascript. Basically the user should be able to input a number into a textfield and "send it" with a button. When the button got pressed i want to run a Javascript function that searches the number in a sql database.
Creating all that stuff shouldnt be a big problem for me, but i have no clue how to create a safe connection between JS and SQL. I have read that a direct connection with javascript is very insecure.
Some people recommend to use java or c# to built an sql connection. How would that work? Basically just an Javascript code, that runs an java/c# application(which builds an sql connection) and returns the needed sql data?
Also heard that its possible to create a sql connection with node.js, is this safe? Or is another method more suitable?
Greetings
I have read that a direct connection with javascript is very insecure
The danger is in giving direct access to your database to the client. JavaScript is most commonly run client-side in web browsers, so for it to access the database you would have to give the browser (and thus the visitor) a username and password on your database server and let them run raw SQL.
There are many possible security risks with this and it just isn't worth it.
(Aside: You can't make arbitrary socket connections with browser-side JavaScript, so it's impossible to connect to most database servers from it anyway).
If you want to expose data to JavaScript running in the web browser, then the standard approach is to write a webservice.
You can write the webservice in any programming language you like (including JavaScript). It listens for HTTP requests, reads data out of them, possibly performs authn/authz, the queries the database (applying the well-documented defences against SQL Injection attacks) and returns the result (often formatted as JSON).
The client-side JavaScript, therefore, just has to make an HTTP request (e.g. with XMLHttpRequest or fetch) with parameters passed in the query string or request body, and process the data it gets back from it.
Connecting to a database using client side javascript is very insecure as the javascript will need to know the login details. And since the client side javascript is on the client side, any user will be able to see the login details in plain text.
The best way to do this is to make a webservice on a server. When the button is clicked it will make a GET/POST request to the webservice with the entered number as a parameter. The webservice, which can be made using any language pretty much, will create the connection with the database and insert the row itself.
Although I would advise going the webservice route since it will be much easier to make secure. Playing with javascript to database is extremely dangerous unless you have a really good system and understand exactly what you are doing; but if you really want to do it and have an application that requires it, then can use PouchDB connected with CouchDB.
PouchDB is run locally and can sync with CouchDB over HTTP.
https://pouchdb.com/
https://couchdb.apache.org/
There is an answer here discussing basic security with pouchDb synchronizing with couchDb. Basically, each person needs separate login credentials and credentials should never be stored in the page code.
PouchDB security
There are some neat uses for pouchDB: https://pouchdb.com/users.html

Building a node.js chat

I'm currently building a web chat with node.js for the backend. It uses web sockets for the communication between server and client. I haven't worked with node before and I always used PHP/MySQL and Ajax to store and retrieve data. The chat can be compared to MSN etc. with user accounts and contact lists.
I was looking for some tips and frameworks, which could help me with developing a website which heavily relies on JavaScript and which does all its communication via node.js.
What would you recommend to store the data? Mongo db (mongoose? mongo-db native?), SQLite? An easy solution would be appreciated.
Would you send everything via the websocket and would you establish a new connection for every conversation?
Is there a way to get around PHP completely?
Would you recommend the usage Backbone.js or Ember.js?
Thanks for you help!
What would you recommend to store the data? Mongo db (mongoose?
mongo-db native?), SQLite? An easy solution would be appreciated.
I would suggest you to use redis, because it is insanely fast.
> Example of benchmark result The test was done with 50 simultaneous
> clients performing 100000 requests. The value SET and GET is a 256
> bytes string. The Linux box is running Linux 2.6, it's Xeon X3320 2.5
> GHz. Text executed using the loopback interface (127.0.0.1). Results:
> about 110000 SETs per second, about 81000 GETs per second.
As client I would use node_redis
Would you send everything via the websocket and would you establish a
new connection for every conversation?
websockets are not yet support by every browser(pointing especially to Internet Explorer ;)). I think you should use socket.io which supports multiple transports so that it will work in every major browser.
Is there a way to get around PHP completely?
You could get around PHP completely. Use for example express as your web framework instead of PHP
Would you recommend the usage Backbone.js or Ember.js?
Also need to have a better look at both of them.
Use mongo-db or couchDB to store the data.
Establishing a new connection is better than sending everything through websockets and parsing them.
Yes. Use node.js on the server side.
You can try either one of them. Both have their pros & cons.

What is the best way to get data from an android app into a remote database? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Remote Database access
I'm developing an app for android which shoult connect itself to a remoe mysql-database.
So far I know what to do to get the app running visually...but how can I transfer the data into a remote database?
What kind of processes have to be developed to get the data into the right shape so that a websserver can translate that and put it into a database?
What do i have to do for getting:
the right structure of the data
transfer/translate/design an interface to bring data to a remote server (--> is it possible to use javascript only)?
What do i have to use? Ajax/JASON etc.
Anyone can help me out on this question of architecture?
You can create some RESTful web services to access the mysql database, then the Android app can call the web service to get or post data to the db, use XML or JSON as the format of data.
It depends on your remote database. If it is CouchDB for example, you just do simple HTTP requests.
If it is SQL you can use JDBC like other client applications would, too.
But perhaps you don't want to have database passwords on your clients. Then you will have to create a web application which is connected to your database. This web application then could provide your favorite (JSON over HTTP) protocol to your android clients.
Take a look at web APIs. Most common is WSDL/SOAP. In regards to the structure, I'd advise you to take a look at JSON. XML works as well, but is pretty redundant. There is also the possibility of using Remote Method Invocation with Java, but that's usually not really worth the effort, I think.

Categories