Share On Twitter Facebook Google+ LinkedIn Pinterest Tumblr Reddit

How to organize files for website using node.js?

Tags: node.js
Question Id:64

I'm working on a defi tracking project as a way to solve a problem I have and learn to code as I go. The goal is a website where the user can enter a wallet address and get the P&L for different positions they have. Zapper does a good job for 80% of what is needed, but there's no P&L calc and no way to see how the returns work.

So, I've started with some simple API calls using covalent and I'm trying to figure out how to organize my files. Right now I'm writing a class to call APIs. Should that be in a folder that has all the code around dealing with API data? Should the API data cleaning be done in separate files? How about the website features like calculating the P&L?

I've heard of the MVC framework, but I'm not sure how to apply it. Any insight is appreciated. Thanks in advance.


Is there something in common about calling all those different APIs, such that some common code helps with all of them?

My position on MVC is that the separation between M and VC is more useful than the separation between V and C. But if you don't yet see that even the first separation will help you, you can skip worrying about it and refactor later if you start to see a need. I tend to like it, on the grounds that business logic is different from dealing with the mechanics of displaying stuff and interpreting inputs and controls from the human.

For global variables or the top-level variables in modules, I tend to use

  • s -- material defined in the current script (some of which may be exported and some not)

  • t -- temporary or testing

  • lib -- library code

  • app -- the application

For layout of files and directories, do what makes sense to you. Some of the distinctions that might figure in, or might not, are, is it application code or library code (general techniques you could see yourself reusing in different applications) and then does it run in the back end, front end, or both.

Small Web Projects

If it is on the smaller side I typically go with something like this:

  • /node_modules

  • /config

    • config.json

  • /libs

  • /routes

  • /public (sometimes called: view)

    • css

    • js

    • img

    • index.html

  • package.json

  • procfile

  • app.js

Node Modules Unless you are writing everything from scratch (which you probably shouldn't) then this is sort of required.  

Config Holds any config or json files that I might need for the project. Typically used for setting up the server.  

Libs is for any libraries I make. An example would be a function that fetches data from a third party API.  

Routes On larger websites with 10+ pages I typically use a routes folder. Common when using expressjs or other similar frameworks.  

Public is the same as your standard website folder. It holds all your HTML files, CSS, imgs, videos, and so on that is open to the public.  

package.json This is used to track what packages you are using. Even if you don't have any it contains other information and most servers require it.  

procfile is a file used for heroku, but a few other server providers use it as well. It tells them which file the server is supposed to run. It looks like this:

web: node app.js

Your Answer

Review Your Answer