Search code examples
node.jsreactjsdirectory-structure

What is an appropriate way to structure the directory of a fullstack application?


To precurse this question I'll mention that this is my first question on Stackoverflow so I'm sorry if I miss out on common practices or miss something else that makes this easier to answer.

My question refers to the way that others structure their full-stack applications. In my case, I'm making a PERN (PostgreSQL, Express, React, Node) Stack application. I've looked around and seen some tutorials that suggest that there should be a separation of concerns for front-end and back-end meaning that there should be a root/ directory that contains a /client/ and /server/ directory with their own package.json, node-module, etc. I'm assuming that the README.md, .gitignore, and other common files are still in the root. Overall, I would like to know what directory structures are common to use and if my presumption on the information given is correct.

Example:

  ├─ client/
  │  ├─ node-modules/
  │  ├─ public/
  │  ├─ src/
  │  ├─ package.json
  │  ├─ package-lock.json
  │  └─ ...
  ├─ server/
  │  ├─ node-modules/
  │  ├─ index.js
  │  ├─ package.json
  │  ├─ package-lock.json
  │  └─ ...
  ├─ README.md
  ├─ .gitignore
  └─ ...

Also, I'm curious about the name dedicated to the Javascript that runs the server. I've seen index.js and server.js


Solution

  • Decoupling the front-end from the back-end is a common approach, especially if you have separate teams developing each. Part of the consideration should be what authentication method you want to use (sessions vs tokens) and also how you want to deploy your app.

    One benefit of having one node server that serves up your front-end is that it's the same domain so you can use sessions. If you decouple it, it will be on separate domains, so you would generally use tokens.

    A benefit of decoupling them is that you can have multiple front-end applications connecting to one Restful API.

    I had this same question a couple months ago and tried both methods with different apps. I really think it depends on your use case, but recommend trying both on a couple personal projects.

    Here is an example of an app that uses the same server to serve up the build folder. https://github.com/StephenGrider/FullstackReactCode/tree/master/server