express-ecma-boilerplate-mongodb v1.0.0
Express ECMA Boilerplate With MongoDB
This is a minimalistic express boilerplate with the following features:
ECMAfeatures enabledDotenvLoad environment variables from .env filePrettierto prettify the codeMongoDBready to go configuration with mongooseCORSfeature enabledPassportlocal authentication strategy and middleware to restrict access.Error Handlingerrors custom middleware and helpers globally configuredMulter File Uploadingmiddleware configured to use in routes as middlewareAxiosglobally configured in./src/utils/api.utils.jsSwaggerdocumentation reachable athttp://localhost:3000/api/v1/docsDockerready configuration with multi-stage optionBest practicesin naming files
Basic Information
App entry point is located in
./src/app.jsServer config entrypoint is located in
./src/bin/www.jsBabel config to transpile the code is located at
./.babel.rcPrettier config is located at
./.prettierrc.jsPassport config is located at
./src/config/passport.config.js- Local Strategy is defined in
./src/services/passport/passport-local.service.js
- Local Strategy is defined in
Mongo config is located at
./src/config/database.config.js- Models definition are located in
./src/services
- Models definition are located in
Error Handling middleware is located at
./src/middlewares/errorHandler.middleware.js- You can configure as many errors you need in
./src/helpers/errors.helper.js
- You can configure as many errors you need in
Swagger config file is located at
./swagger.json- Swagger routes are defined in
./src/routes/swagger.route.js
- Swagger routes are defined in
Docker config is located at
./Dockerfile
Folder Structure
src/
bin/- server configuration folderconfig/- here you find all the configuration files (database, passport, etc...)constants/- here you can put all you global constantscontrollers/- all the controllers to use in routes that interact with serviceshelpers/- some helpers func i.e. an error helper that returns json everytime an error comes inmiddlewares/- here you can find all the custom middlewaresmodels/- database model definitionroutes/- here you find all the defined routes of the appservices/- here we store all the services; i.e. here we define methods to manipulate a db model entityutils/- containing some utils function to be reused in the code (i.e. axios global configuration)
Getting Started
Copy the .env.local to .env
cp env.local .env
# Then replace the string in .env file MONGO_URI with your connectionTo get started with this repo npm install in the root folder
npm installTo getting started with a dev environment. Here we use nodemon and babel-node to restart the server asa we change something
npm run devTo transpile the code and create a production build
npm run transpileThis command will create a build in the root directory
To start with a production ready build you can run this command
# This set the NODE_ENV to production, npm-run-all, create a build and run the server command
npm run startIf you have a build and you want to node the build you can run
# This command launch the node instance inside the ./build/bin/www
npm run serverDocker Ready
Here we use the multistage build to optimize speed and size of the final image
If you use Docker and wanna dockerize the app you can run the command
docker build -t <dockerhubusername>/<docker-image-name>:<tag> .then
docker run --name <docker-process-name> -d - p 3000:3000 <dockerhubusername>/<docker-image-name>:<tag>5 years ago