Project Structure¶
Your project will look like this:
├── package.json
├── public
│ ├── logs
│ | └── access.log
| ├── docs
| | └── swagger.json
| |
├── README.md
├── src
│ ├── app
│ │ └── auth
│ │ ├── AuthRoute.js
│ │ ├── UserController.js
│ │ ├── UserMiddlerware.js
│ │ ├── UserModel.js
│ │ ├── UserRoute.js
│ │ └── UserService.js
│ ├── config
│ │ ├── db
│ │ │ └── connection.js
│ │ ├── environments
│ │ │ ├── config.js
│ │ │ ├── development.js
│ │ │ ├── index.js
│ │ │ ├── production.js
│ │ │ └── test.js
│ │ ├── express-middleware.js
│ │ ├── logger.js
│ │ └── route
│ │ ├── route.index.js
│ │ └── routes.js
│ └── server.js
├── test
│ ├── index.js
│ ├── shared.spec.js
│ └── users
│ └── users.spec.js
src¶
src is the source code directory:
├── src
│ ├── app
│ │
│ ├── config
│ │
│ └── server.js
appcontains all needed componentsconfighas all the configurations likeDB,ENVserver.jsis the app boostrap file
Structure your solution by components¶
Instead of MVC pattern we recommended components based pattern:
├── src
│ ├── app
│ │ └── auth
│ │ ├── AuthRoute.js
│ │ ├── UserController.js
│ │ ├── UserMiddlerware.js
│ │ ├── UserModel.js
│ │ ├── UserRoute.js
│ │ └── UserService.js
authis the component name and that containsroute,Controller,middleware,model, and theservicefiles.
config¶
Config folder structure:
├── src | ├── config │ │ ├── db │ │ │ └── connection.js │ │ ├── environments │ │ ├── express-middleware.js │ │ ├── logger.js │ │ └── route
DBholds the information of MongoDB connectionenvironmentsuse to define ENV variables. We can set different values for different ENV.express-middlewaredeines express middleware need to run this applogger.jslogger details are defined hereroutecreating interface between our components (indeside src/app ) and application.
├── src
| ├── config
│ │ ├── environments
│ │ │ ├── config.js
│ │ │ ├── development.js
│ │ │ ├── index.js
│ │ │ ├── production.js
│ │ │ └── test.js
config.js¶
It holds common ENV variables across all environments. development, production, and the``test`` are extends this file.
route¶
├── src
| ├── config
│ │ └── route
│ │ ├── route.index.js
│ │ └── routes.js
Need to link our components in routes.js like this
import UserRoute from '../../app/auth/UserRoute';
const Routes = [
{
url: 'users',
route: UserRoute,
gaurd: false
}
];
export default Routes;
urlgroup name of api endpointroutecomponent route objectgaurd(optional) if you want to skip JWT verification set false. By default it sets true