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:
- Are you transpiling or compiling anything? If so, you will need source and distribution hierarchies.
- 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.
- 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.