0

I have a small pet project which is a single-page application (Node.js + express.js + EJS views) which also includes client-side javascript. I've started the back-end part of the project structure using a straight-forward project structure, comprised of a ./src structure bundling all server-side javascript and a ./public structure to serve all the static bits. However, it't not clear to me how the client-side javascript should fit in this project structure.

Other stackoverflow questions on Node.js, such as this one, suggest that the server-side code should be stored in ./lib and the client-side code in ./app but to me it doesn't make sense to refer to server-side code as library code. Other sources, such as non-authoritative blog posts, replace ./lib with ./server and call it a day. However, that doesn't look like an improvement.

As the client-side javascript is served alongside EJS views it doesn't make much sense to split the project into server-side and client-side projects.

With this in mind, what's the best way to organize a project structure for a single-age application based on Node.JS+EJS+template views?

RAM
  • 2,257
  • 2
  • 19
  • 41

1 Answers1

0

With the mentioned structure of dividing your project in ./client and ./server I already saw it been used with single-page application on React projects. But this demand to run node twice or use a new package to take care of it.

I'm using the same Stack as you and I recommend the same as said in the other topic, to professional express applications, make it simple using:

/
  /bin - scripts, helpers, binaries
  /lib - your application
  /config - your configuration
  /public - your public files
  /test - your tests

Now there is other option where I recommend to leave your server-side files, .gitignore and .env at the root folder. And for the rest you put all into a client folder or whatever the name you choose for your app. For a quickly update at your project, you can add the package Express Generator and install it globally. It currently comes with the following dependencies:

  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "ejs": "~2.6.1",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "mongodb": "^3.6.6",
    "morgan": "~1.9.1"
  }

Using this command: express -e client

You should end up with a project structure like this:

client/
  /bin - scripts, helpers, binaries
  /public - your public files
  /routes - exporting your home route
  /views - your views and partials for EJS

There's also famous projects that you can be consulting to build the pattern of a huge node.JS project with the needed documentation, here is one Sails JS.

pebueno
  • 31
  • 2
  • 7