Node.js application structure - node.js

In my Node.js app, I have a folder for API, Lib, and Utils in addition to Server.js and app.js files.
Is there a structure that is best for a Node Application that makes multiple api calls to different endpoints? I'm struggling with how to best organize the code in my applcation.

Folder structure for your application is already a step in the correct path!
Use an app layer, controller, service, and data access layer for your application. Ensure you name your folders so it's readable. Check out this article for more details - https://blog.logrocket.com/the-perfect-architecture-flow-for-your-next-node-js-project/
One common setup is to have the entry points/initiators call services to do the heavy-lifting work, so you could have a top-level directory called that. This is definitely common at Directly. The two top-level services that stand out would be directlyService.js and stackOverflowService.js (or forumService.js or some other non-vendor-specific name). Those two services could call other services (hopefully there are obvious groupings of the other business processes) to subdivide processing further.

Related

How to share DB schema when there are two backend servers?

I am developing some server. This server consists of one front-end and two back-ends. So far, I have completed the development of one back-end, and I want to develop the other one. Both are express servers and db is using mongodb. At this time, I am developing using the mongoose module, and I want to share a collection (ie schema). But I have already created a model file on one server. If so, I am wondering if I need to generate the same model file on the server I am developing now. Because if I modify the model file later, I have to modify both.
If there is a good way, please let me know with an example.
Thank you.
I have two answers for you one is direct and the other will to introduce the concept of microservice.
Answer 1 - Shared module (NPM or GIT)
You can create an additional project that will be an NPM lib (It can be installed via NPM or git submodules).
This lib will expose a factory method that will accept the mongoose option and return the mongoose connection.
Using a single Shared module will make it easier to update each backend after updating the DB (A bit cumbersome if you have many backends).
Answer 2 - The microservice approach
In the microservice approach, each service (backend) manages its own DB and only it. This means that each service needs to expose an internal API for other services to use.
So instead of sharing lib, each service has a well-defined internal API that other services can use.
I would recommend looking into NestJS (NodeJS microservice framework) to get a better feel of how to approach microservice
It goes without saying that I prefer Answer 2 but it's more complex and you may need to learn more before giving it a go. But I highlight recommend it because microservice (If implemented right) will make your code more future proof.

Modular Nodejs application Set

I want to create a modular node.js application stack containing a set of applications. The idea is that app1, app2, etc can use the controllers and models.
Inside each app folder, I can have app specific package.json, app.js, etc.
I am using express.
I have two issues:
Is it possible to have that structure?
Why I'm not able to deploy such an app set on GCP? When I try It throws 500 internal server error.
enter image description here
To create a similar architecture, even if I didn't find a way to have the same, you should use services. According to the official GAE doc:
Use services in App Engine to factor your large apps into logical
components that can securely share App Engine features and communicate
with one another. Generally, your App Engine services behave like
microservices. Therefore, you can run your whole app in a single
service or you can design and deploy multiple services to run as a set
of microservices.
Does this work for your use case?
Regarding question 2, you didn't provide any information about your current process, so I cannot help you. Please edit the question adding the deployment configuration (app.yaml, etc.) and how is it performed. Please delete any sensitive information before posting it.

sharing code between microservices

I have a suite i'm working on that has a few micro-services working togther.
I'm using Docker to setup the environment and it works great.
My project components are as follows:
MongoDB
Node.js worker that does some processing on the DB
Node.js Rest API that serves the user
As you can probably guess the 2 Node.js servers are suppose to work with the same DB.
Now I've defined my models in one of the projects but I'm wondering what is the best practice when it comes to handling the second.
I would really love to avoid copy pasting my code because that means I have to keep both of them up to date when I do changes to the Schema.
is there a good way to share the code between them?
my project looks like this:
rest-api // My first Node.js application
models
MyFirstModel.js // This is identical to the one in the worker/models folder
MySecondModel.js
index.js
package.json
Dockerfile
worker // My second Node.js application
models
MyFirstModel.js
MySecondModel.js
index.js
package.json
Dockerfile
docker-compose.yml
Any input will be helpful.
Thanks.
Of course you can.
What you have to do is to put your common files in an volume, and share this volume with both Node containers.
You should setup a data volume in which you put all the files you want to share. More about this here or anywhere else by googling it.
Cheers.
The common opinion is the following: two microservices should not share same data model. There are several article about it and some question related to this topic.
How to deal with shared models in micro service architectures
However I think there are some cases when you need it and acceptable. Trust is a luxury even if everything is internal, thus security and conformity must be considered. Any incoming object must be normalised, validated and checked before initiate any process with it. The two service should handle the data with the same way.
My solution that I used for an API and an Admin services which shared the models:
I created 3 repositories, one for the API and one for the Admin and a 3th one for the models directory. Models should be present in both repositories so and I added it as a git submodule. Whenever you change something on a schema, you should commit it separately, but I think it is the best solution to manage the changes without duplicating the code.

Best way to separate logic in sailsjs (nodejs)

My application structure consist of few parts. Public API, Admin API, Admin Console (GUI), Private API, Auth API (oauth2, local, socials), etc. They kinda different each to other, but using the same models. Some routes going to have high number of requests per second and couldn't be cached.
I'm interesting in best practices to split everything properly. I'm also opened to another frameworks or even io.js.
Right now I got 3 variants:
Create separate apps.
Group controllers by folders, and find a way to group routes.
Create another instance of sails app and run it into another process (So I can have all controllers, models, but how should I organize subapp structure using this way?)
I think most answers will be opinionated, but putting controllers into subfolders is the easiest way to share models. (easiest but not only)
And you can easily run policies based on those subfolders as well.
However you really need to flesh out more aspects of your question and think about if there will be more shared (like templates or assets) than different or if differences would prohibit a shared app. Will they all use sessions or will they even use the same sessions.
In the end, based on your limited question, sails can do what you want.

Separating socket.io namespaces into separate files

I've written two relatively large socket.io apps, one for playing a game and the other for chat, which I've separated into two namespaces.
I would like to now move these out of my main file app.js into some namespace directory, and just require them in my express app leaving all of the functionality intack.
How would I go about this, or is there some way to get the effects of what I'm looking to do in some other manner?
In order to use separate files you need to use modules in node.js and use require to load them.
Modules have special structure and syntax to follow, in order to be able to call modules functions and interact with it.
Read about modules here: http://nodejs.org/docs/latest/api/modules.html
If you need to interact with functions, objects and data inside of the module, then it might be a lot of work on remaking architecture of your application.
This is something that you have to take care from the earliest moments of the development, in the process of architecture and technical design of application.
To use same socket, you have to initialize it in parent module that require child modules, and pass socket app handle to those child nodes, that they will be able to use it.
The worst and straight forward option to make it, and is absolutely not the option in commercial world, is to load js file content and just eval() it. But remember - this is absolutely not recommended and in commercial world you should never use it.

Resources