How to connect manually to MongoDB Database in loopback? - node.js

I am trying to connect MongoDB database manually. So how is it possible to get the collection in MongoDB. I need to implement the code in custom js file and not in bootscript. If I include server.js file the problem is the entire application including the web server, boot scripts, etc, runs for the duration of the script, which means I'd have two instances of the app running.
So is there any way to manually integrate MongoDB and fetch documents in a collection.
Any help would be really appreciated.
Thanks in Advance

Set app variable to global and when bootscript is loaded first, set that global app variable to true and put an if condition for checking global app variable. If its set to true then return. Hope this will fix your issue.

Related

How to execute shell scripts on the backend of a nodejs web app?

I have a full stack MERN application running on a google compute engine, along with a local mongodb running on the same server. Its a CRUD application, however, I have a script that is stored in the server that I would like triggered everytime a user presses a button on the front end. (a use case would be, user enters some input that is logged into the database, and when its logged I would like a script triggered on the backend that creates a json file out of the mongodb table and uploads it to github/emails it out).
I'm not sure where to start learning this, a few google searches have led me to AJAX and child_processes, am I going in the right direction? Any resources or pointers would be great. Thank you
If I understood the question correctly then you want to accomplish the following things:
1. Export json data from a local MongoD instance.
2. Then send that data to github or email it somewhere.
In that case I would recommend to use child process(exec, spawn, execFile, fork) to execute the mongoexport command to get .json files.
But I don't recommend using shell script to upload that data to github or email it.
Use the github api for github and use node-mailer to email the data.
To learn more about child processes read the docs here Node.js v14.x Child process docs

Nodejs config which is subject to change

I was wondering what the best way is to save config values in nodejs/express which are subject to change. I'm not talking about database credentials that you store inside your environment variables. But say a game where the config values are changeable within the admin panel. A config for example could be how often in ms a player receives a random reward. I could store this directly into the code (but I'd have to alter my code everytime I would like to change this and this cant be changed from within an admin panel), have some different file that has an object with these configs or save it into a mysql database (I would need to query the db everytime I need a config though).
I'm not really sure what the best way is to do this. Basically I want a way to store flexible configs within my nodejs application that are subject to change.
Thanks in advance.

how to get my front-end updated when I add to my database?

I'm designing a real-estate website and I was wondering can I link my front-end webpage that shows the available properties for sale that a new property icon is added when I add to my MongoDB ? to elaborate more, I have a template for each single property which includes picture and some other info about the property, what i want to do is when i add a new collection to my database to appear on my webpage with this template. Thanks in advance
MongoDB can be watched, since your question has 'node.js' tag so assume you use node.js in the server side, I check the doc and there's a watch API returning a ChangeStream object which you may use, just for your reference
If you want to use angular for retrieving and showing data.
You can refer to these sites :
This one tells how to populate data in angular.
&
To get a crisp of doing it with mongo db
What you actually need is server side event system updating your frontend, Changestream that #user1108069 mentioned can be useful in detecting new changes or you can literally identify that when a request is being made to add a new property.
The task here is to update the frontend's current connections to update their view to get the new (load) changes, you can do this in multiple ways.
Use something like firebase realtime DB on the side Firebase Realtime DB
Use any other server side events broker for you This should work too
You can create your own full duplex system, using something like socket.io but do remember that concurrent connections needs to be handled properly. You cannot keep them connected to your server all the time.
Cheers!

how to access a different module in multi target application

I'm new to cloud foundry, so I'm not sure, if my thoughts and plans are right. Maybe someone can explain or discuss it with me.
What I want to do:
Implement a MTA (Multitarget Application) with a a html5-module as frontend and a nodeJS-module as backend. Furthermore there should be a mongodb instance, which will be accessed from the nodejs-module. Later it should also get multitenant.
What I already did:
I implemented a simple nodejs-app and connected it to the db. Persisting and calling data with rest works already fine. I implemented a simple sapui5 app, which consumes data from the db with ajax. For now, the node startscript is in the html5 module, so it works somehow. But now I want to separate the modules.
So I created a mta-project with the two modules in webide and imported the two apps.
What I expect to do for it:
For now, I have an approuter, which is in my nodejs-module, but I can not access the webapp folder in the html5-module from here: file not found error: /home/vcap/app//. Is there a possibility to access the webapp-folder in another module over the path "/home/vcap/app/"? Or can I lookup the app-directory anywhere?
I have read, that an approuter-module (nodejs) can be needed, but I don't know exactly what it does. I think it serves the index.html file when opening the url of the whole app?

store string inside machine without using Database

I have created NodeJS application that communicate with SerialPort for example COM7.
because the COM name is not static it could be change all the time.
I'm looking for a way to save COM7 as a String inside a machine, and able to fetch it to NodeJS code.
and also change it remotly.
It is the same idea like database, but I see it really unnecessary to start database for only 1 record.
Look like Session on php but permanent.
** OS: Raspbian.
Thanks.

Resources