It's an opinionated boilerplate for Node web APIs focused on separation of concerns and scalability.
- Multilayer folder structure
- Code organization inspired by DDD and Clean Architecture
- Instant feedback
- Use Nodemon to automatically reload the server after a file change when on development mode, makes the development faster and easier.
- Ready for production
- Setup with PM2 process manager ready to go live on production. If you use Heroku there's also a Procfile and a post-build task to run the migrations on production.
- Scalable and easy to use web server
- Use Express for requests routing and middlewares. There are some essential middlewares for web APIs already setup, like body-parser, compression, CORS and method-override.
- Database integration
- Sequelize, an ORM for SQL databases, is already integrated, you just have to set the authentication configurations.
- Prepared for testing
- The test suite uses Mocha/Chai and is prepared to run unit, integration and functional tests right from the beginning. There are helpers to make it easy to make requests to the web app during the tests and for cleaning the database after each test. A FactoryGirl adapter for Sequelize is setup as well to make your tests DRY, and the tests generate code coverage measurement with Istanbul.
- Dependency injection
- With Awilix, a practical dependency injection library, the code will not be coupled and it'll still be easy to resolve automatically the dependencies on the runtime and mock them during the tests. It's even possible inject dependencies on your controllers with the Awilix Express adapter.
- CLI integration
- Both the application and Sequelize have command-line tools to make it easy to work with them. Check the Scripts section to know more about this feature.
- Logging
- The Log4js logger is highly pluggable, being able to append the messages to a file during the development and send them to a logging service when on production. Even the requests (through morgan) and queries will be logged.
- Linter
- It's also setup with ESLint to make it easy to ensure a code styling and find code smells.
Notice that the boilerplate comes with a small application for user management already, you can delete it with a npm script after you understand how the boilerplate works but please do the quick start first! 😊
- Clone the repository with
git clone --depth=1 https://github.com/talyssonoc/node-api-boilerplate
- Setup the database on
config/database.js
(there's an example file there to be used with PostgreSQL 😉 ) - Install the dependencies with
yarn
(click here if you don't have Yarn installed) - Create the development and test databases you have setup on
config/database.js
- Run the database migrations with
npm run sequelize db:migrate
- Add some seed data to the development database with
npm run sequelize db:seed:all
- Run the application in development mode with
npm run dev
- Access
http://localhost:3000/api/users
and you're ready to go!
After playing a little bit with the boilerplate and before implementing a real application with it I recommend you to read at least the Setup
and the Organization and architecture
sections of our Wiki. After that you'll be able to remove the example application files running npm run cleanup
- Don't forget to run the migrations for the test environment as well (including when you create a new migration) with
npm run sequelize db:migrate -- --env=test
This boilerplate comes with a collection of npm scripts to make your life easier, you'll run them with npm run <script name>
:
dev
: Open the application in development modestart
Open the application ready to production (prefer not to do that in development)test
: Run the test suitecoverage
: Run the test suite and generate code coverage, the output will be oncoverage
folderlint
: Run the lintersequelize
: Alias to use the Sequelize CLIconsole
: Open a pre-built console, you can access the DI container through thecontainer
variable once it's open, the console is promise-friendlycleanup
: Removes the files from example application
- Node v6+
- Express
- Sequelize
- Awilix
- Structure
- HTTP Status
- Log4js
- Morgan
- Express Status Monitor
- Nodemon
- PM2
- Mocha
- Chai
- FactoryGirl
- Istanbul
- ESLint
It's the first version of the documentation of this boilerplate, feel free to contribute to it.