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.
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:
You can use Gitpod for the one click online setup. With a single click it will launch a workspace and automatically:
bulletproof-nodejs
repo.cp .env.example .env
.npm run start
.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.
route.post('/signup',
celebrate({
body: Joi.object({
name: Joi.string().required(),
email: Joi.string().required(),
password: Joi.string().required(),
}),
}),
controller.signup)
Example error
{
"errors": {
"message": "child \"email\" fails because [\"email\" is required]"
}
}
Read more about celebrate here and the Joi validation API
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
.
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
I know this is not a perfect architecture but it's the most scalable that I know with less code and headache that I know.
It's meant for small startups or one-developer army projects.
I know if you start moving layers into another technology, you will end up with your business/domain logic into npm packages, your routing layer will be pure AWS Lambda functions and your data layer a combination of DynamoDB, Redis, maybe redshift, and Agolia.
Take a deep breath and go slowly, let the business grow and then scale up your product. You will need a team and talented developers anyway.
问题内容: 为了在工作中进行演示,我想比较NodeJS和C的性能。这是我写的: Node.js(for.js): 我使用GCC编译for.c并运行它: 结果: 然后我在NodeJS中尝试了它: 结果: 在运行了无数次之后,我发现无论如何它都是成立的。如果我将for.c切换double为long在循环中使用a而不是a ,则C花费的时间甚至更长! 不是试图发动火焰战争,但是为什么执行相同操作的Node
问题内容: 将felixge的mysql用于node.js时,如何向结果对象询问返回的行数?我有一个相当昂贵的查询,所以我不想先运行,只是想再运行一次查询。 问题答案: 如果这是一个选择查询,则只需取返回数组的长度即可。 如果是更新/删除查询,则返回的字典将具有受影响的行变量。
问题内容: 我正在尝试在NodeJS中使用MySQL。我的整个应用程序都是基于Promise构建的,因此我也想推广该模块。 所以我有这个: 现在,根据他们的API,该方法接受一个参数,即在发生连接错误时调用的回调。我的问题是,这如何转化为承诺? 承诺会因错误而解决吗?会被拒绝吗?我可能需要吗?这是如何运作的? 问题答案: 如果某个方法是带有单个参数的节点“ errback”-它将在中没有任何参数地
问题内容: 我最近在nodejs中从memcached切换到Redis。我在node- memcached中喜欢的事情是我可以将整个javascript对象保存在内存中。可悲的是,我无法在Redis中做到这一点。例如,我得到以下对象: 使用3rd-Eden / node-memcached我可以做到: 然后 然后,我将按原样获得保存的对象。 Redis的问题在于,如果我像这样保存对象: 当我获得价
问题内容: 在Redis中,我通过CLI运行Lua脚本,如下所示:- 因此,我的Lua脚本接受4个键和2个参数。 现在,我想在Node.js中运行相同的脚本。 我正在使用此库在我的应用程序中导入Redis。 我没有找到任何有关执行Lua脚本的函数参数的示例。 因此,我只是碰到一些可能起作用的东西。但是似乎没有任何作用。 我的app.js像这样: 我的问题:如何使用node.js执行以下命令,以便它
问题内容: 将NodeJS与MongoDB + Mongoose结合使用。 首先,我知道异步非阻塞代码的优点。所以我确实处理回调。但是最后我遇到了以下问题。 可以说我有一个可以随时被用户调用的函数。超级“闪电般的”用户可能几乎同时调用两次。 当然它是这样执行的:查找查询,查找查询,保存查询,保存查询 这完全破坏了应用程序的逻辑(应查找查询,保存查询,查找查询,保存查询)。因此,我决定通过“锁定”特