A simple nodejs application with MVC and API
suggest change- The first major distinction is between the dynamically generated directories which will be used for hosting and source directories.
- The source directories will have a config file or folder, depending on the amount of configuration you may have. This includes the environment configuration and business logic configuration, which you may choose to put inside a config directory.
|-- Config
|-- config.json
|-- appConfig
|-- pets.config
|-- payment.config
- The next directories distinguish the server-side backend from the frontend modules. The two directories server and webapp represent the backend and frontend, and can live inside a source directory such as src.
You can choose different names for server or webapp, depending on what makes sense for you. Don't make the name too long or too complex, because this is internal project structure.
- Inside the server directory you can have the controller, and App.js or index.js, which will be your main Node.js file and starting point. The server directory can also have the dto directory, which holds the data transfer objects used by API controllers.
|-- server
|-- dto
|-- pet.js
|-- payment.js
|-- controller
|-- PetsController.js
|-- PaymentController.js
|-- App.js
- The webapp directory can be divided into two major parts, public and mvc. This depends on the build strategy you want to use. Here we use browserify to build the MVC part of the webapp and minify the contents of the mvc directory.
|-- webapp
|-- public
|-- mvc
- The public directory can contain all the static resources, images, CSS (you can have Sass files as well), and, most importantly, the HTML files.
|-- public
|-- build // will contain minified scripts (mvc)
|-- images
|-- mouse.jpg
|-- cat.jpg
|-- styles
|-- style.css
|-- views
|-- petStore.html
|-- paymentGateway.html
|-- header.html
|-- footer.html
|-- index.html
- The mvc directory will contain the front-end logic, including the models, the view controllers, and any other utils modules you may need as part of the UI. index.js or shell.js, whichever suits you, is part of this directory as well.
|-- mvc
|-- controllers
|-- Dashboard.js
|-- Help.js
|-- Login.js
|-- utils
|-- index.js
In conclusion, the entire project structure will look like the tree below. A simple build task such as gulp browserify will minify the MVC scripts and publish them in the public directory. That directory can then be served as a static resource with express.use(express.static('public')).
|-- node_modules
|-- src
|-- server
|-- controller
|-- App.js // node app
|-- webapp
|-- public
|-- styles
|-- images
|-- index.html
|-- mvc
|-- controller
|-- shell.js // mvc shell
|-- config
|-- Readme.md
|-- .gitignore
|-- package.json
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents