I want to dockerize a REST Api which uses multiple microservices. Most tutorials only cover very simple cases so I am a bit stuck about how to properly dockerize such a project.
My project structure:
node_modules
src
request-manager
request-worker
rest-api
shared
test
package.json
tsconfig.json
So it has three microservices (request-worker, request-manager and the rest-api).
My question:
Is a .dockerignore and Dockerfile for each microservice necessary or how would one properly dockerize such a mono repository project?
You can create one Dockerfile per microservice, but will probably have several conflicts and problems due to the fact that they are sharing multiple files (node_modules especialy).
I recommand use one (sub)repository for each service:
request-manager
Dockerfile
src
node_modules
test
package.json
tsconfig.json
request-worker
Dockerfile
src
node_modules
...
rest-api
...
shared
And be very careful at what you put in shared folder.
You also can use docker-compose for managing dependancies.
Related
I'm creating a codebase and from this codebase I want to build several tools: a CLI, and an API, all within the one repository as a mono-repo so I don't need to publish my package. What is the project structure convention in NodeJS projects to achieve this?
My current setup looks similar to this:
myProject/
- package.json
- src/
| - index.js
- packages/
| - cli/
| - api/
The CLI and API both consume src/index.js as an example. The root level package.json contains all the dependencies.
I'm not too experienced with NodeJS, what should be the best approach here? I think I'll need to give the API and CLI their own package.json files as they're technically separate from the main project package?
Could I treeshake node_modules when i build a nodejs backend project?
The node_modules is so big and there are too many small files in it,How can i treeshake it like the frontend do?
Too many small files in node_modules is not used in my project but when i want to deploy offline I must carry them from here to there.
node_modules
-- ramdajs
-- lodashjs
-- ...
How can I do treeshaking with node_modules in an express project.
i have a project with two nodeJS projects inside, a backend project with inside a folder "client" for the frontend.
Now i would like to make one common node_moduels folder both.
I found that PNPM could do that, but for me the documentation is not so clear. How doest it work?
How can i run pnpm i on my root folder and then make my node_modules accessable for my "client" folder?
Thank you!
On Linux or Mac, i usually symlink the module (ln -s) into my other projects' libs. Then i import or require the module from there. It's located directly in your codebase, not in node_modules, but it works just fine.
Just don't update the common module without keeping in mind all the dependant projects.
For easy versioning, you may choose to publish your module to npm (even as private), and update it with each push for each of the dependants.
Recently I've done a few small projects with a Node backend and a React frontend using create-react-app. Although I've spent quite a bit of time researching best practices for folder structures I haven't been able to come up with a satisfying solution without having to eject the react app. I wonder if there are solutions out there that I just haven't found yet.
The folder structure I am trying to aim for looks something like this:
package.json
src/
client/
app.js
...
server/
index.js (node.js main file, could just as well be in server/)
This layout would allow me to run all scripts from the root folder and I can put all configurations in one package.json. For example, I would like to be able to just run npm test from the root folder which would then use jest to run both the client and the server tests.
As far as I can tell create-react-app is hardwired to expect a src/index.js when using npm start, which prevents me from using this folder structure - if I just move all the boilerplate generated by create-react-app into the client folder I have an ugly src/client/src folder which again contains it's own package.json. I guess I could split it differently, creating a client/src and server/src, but I still have separate package.json files in each directory.
Would love the hear about your experiences with this, maybe I am missing the obvious solution.
mainDir/
app/
src/
index.js
package.json
api/
src/
index.js
package.json
I follow this pattern for developing applications in microservice architecture.
This structure is for mono-repos.
You also can divide the front end and backend into different repositories.
I have a project in which I use node-webkit. node-webkit allows npm packages to be used for developing desktop applications. I make use of grunt to build my application.
My folder structure looks like this at the moment:
project root
node_modules/ (1)
package.json (1)
App/
node_modules/ (2)
package.json (2)
bower.json
bower_components/
...
controllers/
filters/
...
app.js
The npm dependencies for the application itself are kept within the App folder, but the dev dependencies for building the project are not related to the application source code, so i keep them in node_modules (1) inside the root folder. I also know that in a package.json file one can express dependencies and dev dependencies, exactly for this reason. I would rather have one package.json file in the root expressing ALL dependencies, including dev dependencies, but i would rather have a separation of those dependencies on folder level.
Two questions arise:
Is this a good way to organize my npm dependencies? If yes, awesome? If no, which I expect:
What is a better way to organize my dependencies? Is it possible to specify that dev dependencies go into folder a, and 'regular' dependencies go into folder b? If so, how do I do this?
In case anyone is wondering, this is the project i am talking about:
https://github.com/michahell/pinbored-webkit
[updated folder structure to include app.js for clarity]
It is perfectly fine to keep more than one package.json file and multiple node_module directories for a project. If you consider the parts as separate components.
An example might be if, you have one directory containing a node server, another containing a react app, and a third containing some kind of deployment script written in javascript.
#Michael package.json file contains all the dependencies related to that project.There is no need for multiple package files and multiple node_modules folders..
But you need to check where is your App.js file!!
your App.js , package.json must be in same folder unless configured.