0

I am making a node.js app with socket.io and am wondering what the proper folder structure is. I have these files.

  • Server.js
  • package.json

and

  • Client.js
  • Index.html

I also have some images (png) that I want to be displayed in my index.html. (https://www.tutorialsteacher.com/nodejs/serving-static-files-in-nodejs) Some folder names that I have seen are public, static, server, and app, but I'm not sure what each one if for. My app uses socket.io, http, path, and express.

This is my first question so I'm sorry if it is not formed properly.

Thanks.

Jy T
  • 86
  • 8

2 Answers2

1

There's not a "good for all" project folder structure. It really depends on the project you are working and can be strongly influenced and mutated by architectural decisions.

For a small project, and learning purposes, you can use a "standard" folder structure such as the one that Express.js uses in their express-generator, or you can use something similar as the one mentioned here.

For single-person projects, I would advise you to keep it simple and just trying to place some boundaries between modules with notorious different responsibilities, such as routers, database models, tests, assets, templates, and bussiness logic related services. Enough to keep it organized and try to start thinking how to maintain decoupled components while learning.

For bigger projects, I would advise you to do further research on the topic, Clean Architecture by Robert C. Martin is an amazing book for you to dive deep in Software Architecture and come up with a evolving project structure that better fits your needs and the project's.

Nacho H
  • 128
  • 1
  • 9
1

How you organize your server-side files is totally up to you and there are many different way to do it and no single "right" way or "best practice" way. For deployment convenience you just want a consistent relative location between all the pieces so that they can all use relative paths to refer to each other and are not dependent upon any absolute paths.

The one thing that absolutely should be true is that your public files (things that are sent to the client) should generally be in their own hierarchy that can share a common parent with the server files, but the server files should not be inside the public hierarchy. So, something like this would be fine:

 project
      server
           server.js
           package.json
      public
           client.js
           index.html

Depending upon how many public files you have and who else might be working on them, it's common to separate them by type:

project
    server
         server.js
         package.json
         node_modules
            various modules you're using in your server code
    public
         scripts
             client.js
         html
             index.html
         images
             logo.png
         style
              main.css
    database
         database stuff here

This works because the server and public hierarchies are separate. This helps with a whole bunch of things and makes it completely clear as you're developing which things are shared publicly and which things are not. It also allows you to use things like express.static() which automatically serve files from directories based on a name match between route requested and filenames in the directory. Those types of automatic routing must be from a designated public directory or they run the risk of accidentally serving up private server files.

If you had database files, you might put them in another directory at the same level as the server.

How exactly you lay out the server files or the public files within their hierarchies is entirely up to you and what serves you needs as long as you keep them separate.

Some points to consider:

  1. Are you transpiling or compiling anything? If so, you will need source and distribution hierarchies.
  2. Is this now or in the future likely to be a multi-person project. If so, are there people on the project that should only have access to certain parts of the project (such as GUI designers that maybe only have access to HTML, CSS and image files)? If so you probably want to structure things so offering that access is simpler.
  3. Are you using express.static() or something to automatically serve certain types of files. If so, they need to be cleanly isolated in a directory or directories that makes using express.static() easy and safe.
jfriend00
  • 683,504
  • 96
  • 985
  • 979