santiq /bulletproof-nodejs
Implementation of a bulletproof node.js API 🛡️
★ 5,776⑂ 1,143👁 112⑃ 78● 73▤ 0
+1
Daily
5,776
stars
1,143
forks
112
watchers
78
prs
73
issues
0
releases
README
Bulletproof Node.js architecture 🛡️
This is the example repository from the blog post 'Bulletproof node.js project architecture'
Please read the blog post in order to have a good understanding of the server architecture.
Also, I added lots of comments to the code that are not in the blog post, because they explain the implementation and the reason behind the choices of libraries and some personal opinions and some bad jokes.
The API by itself doesn't do anything fancy, it's just a user CRUD with authentication capabilities.
Maybe we can transform this into something useful, a more advanced example, just open an issue and let's discuss the future of the repo.
Development
We use node version 14.9.0
nvm install 14.9.0
nvm use 14.9.0
The first time, you will need to run
npm install
Then just start the server with
npm run start
It uses nodemon for livereloading :peace-fingers:
Online one-click setup
You can use Gitpod for the one click online setup. With a single click it will launch a workspace and automatically:
- clone the bulletproof-nodejs repo.
- install the dependencies.
- run cp .env.example .env.
- run npm run start.
API Validation
By using celebrate, the req.body schema becomes cleary defined at route level, so even frontend devs can read what an API endpoint expects without needing to write documentation that can get outdated quickly.
js
route.post('/signup',
celebrate({
body: Joi.object({
name: Joi.string().required(),
email: Joi.string().required(),
password: Joi.string().required(),
}),
}),
controller.signup)
Example error
json
{
"errors": {
"message": "child \"email\" fails because [\"email\" is required]"
}
}
Read more about celebrate here and the Joi validation API
Roadmap
- [x] API Validation layer (Celebrate+Joi)
- [ ] Unit tests examples
- [ ] Cluster mode
- [x] The logging _'layer'_
- [ ] Add agenda dashboard
- [x] Continuous integration with CircleCI 😍
- [ ] Deploys script and docs for AWS Elastic Beanstalk and Heroku
- [ ] Integration test with newman 😉
- [ ] Instructions on typescript debugging with VSCode
API Documentation
To simplify documenting your API, we have included Optic. To use it, you will need to install the CLI tool, and then you can use api exec "npm start" to start capturing your endpoints as you create them. Once you want to review and add them to your API specification run: api status -- review.
FAQ
Where should I put the FrontEnd code? Is this a good backend for Angular or React or Vue or _whatever_ ?
It's not a good idea to have node.js serving static assets a.k.a the frontend
Also, I don't wanna take part in frontend frameworks wars 😅
Just use the frontend framework you like the most _or hate the least_. It will work 😁
Don't you think you can add X layer to do Y? Why do you still use express if the Serverless Framework is better and it's more reliable?
I know this is not a perfect architecture but itTop issues
#31How could you prevent controller callback methods from becoming too big?10 comments · 2019-08-14#45TS error Logger.error7 comments · 2019-11-11#27getting Property 'error' does not exist on type '{}'7 comments · 2019-08-04#65where are all the "beautiful" unit tests?5 comments · 2020-03-27#3Event dispatch, npm repository, has been archived. Is there a maintained alternative?5 comments · 2019-04-24#80How to deploy the code in Azure Web App4 comments · 2020-08-25#77How would you break this structure in two micro-service if needed later4 comments · 2020-07-18#105Implementation with Mysql/pgsql with typescript3 comments · 2021-07-06