DEV Community

Discussion on: How to Structure Your Backend Code in Node.js (Express.js)

Collapse
 
florianrappl profile image
Florian Rappl

While the micro scheme is fine, it lacks a proper repository structuring.

As an example, not aggregating the source code in a src directory is a huge misstep. You might want to use TypeScript or some other tooling (e.g., a bundler) to process your code for performance benefits. As such putting everything into the root / mixing with your tooling and tests is something you want to avoid strongly.

(You should not use CommonJS - use ESModules; especially if you really care about "maintainability, scalability, and collaboration".)

Collapse
 
adesoji1 profile image
Adesoji1

if i want to convert the javascript application in ES module to a desktop application using electronjs, how do i do it?

Collapse
 
vyan profile image
Vishal Yadav

using tools like Electron Packager or Electron Builder.

Collapse
 
florianrappl profile image
Florian Rappl

Electron.js understands ESM (electronjs.org/docs/latest/tutoria...) - but if you want to only use ESM in development and another format (e.g., CommonJS) at runtime I recommend using a tool such as esbuild (which can bundle and transform).

Thread Thread
 
dev_dive profile image
Chibueze Onyekpere

Thanks for the new insight.

Collapse
 
clarenceliu86 profile image
Clarence Liu

I agree, this is fine for JS, but any serious project should really be using Typescript as you said, type-safe SDKs are super powerful, see Tanstack Router or others that are built with this in mind.

Collapse
 
asisshukla profile image
Ashish Shukla

That’s well said but if we are really aiming that big and complex applications, then we should use another frameworks like nestjs which provide all these things out of the box.

Collapse
 
adesoji1 profile image
Adesoji1

nice work, thank you for this, for a react frontend that has typescript in it, how should the folder structure be arranged?

Collapse
 
florianrappl profile image
Florian Rappl

Just put everything in a source and write the code as-is. (e.g., then src/index.tsx instead of a index.js or src/index.js). Use a tool such as esbuild to consume the source files and transform (+ bundle) them into a dist folder (src/index.tsx and related files -> dist/app.js).

It's a standard practice that pretty much all applications today follow.

Collapse
 
vyan profile image
Vishal Yadav

I will share soon!

Collapse
 
jjco790923 profile image
Juan José Cruz Ortiz

you are wrong since the moment you want to put types to a non typed language, if want types go and do you stuff in c# or java

Collapse
 
florianrappl profile image
Florian Rappl

My comment was independent if you like or want to use TypeScript. That was one example. Having a src directory is pretty much always helpful (as you might want to bundle for performance reasons).

Maybe instead of making useless comments it would be great to improve your knowledge about JS and its ecosystem from time to time. Might help.

Thread Thread
 
jjco790923 profile image
Juan José Cruz Ortiz

If you had a good understanding of js you wouldn't have to use typescript in the first place, and secondly you've obviously never worked on any big enough project where the ShitTypescript spends 2 hours making bundles for perforance reasons at every line change

Thread Thread
 
nguyendat251 profile image
NguyenDat251

Working on a big project without needing Typescript? You are the one who sounds like you have never worked on any big one. That is Typescript's purpose: to add typing syntax for js, to let the code document itself. So when the project is scaled up, people can join and easily follow up the codebase.

Thread Thread
 
vyan profile image
Vishal Yadav

Yeah , you are right , I'm solo freelancer! but it will help who is new in this field.