fastify 后台
Fast and low overhead web framework, for Node.js
快速,低开销的Web框架,用于Node.js
Fastify version 1 was released on March 7th. This post will show you how to get it set up, and we’ll discuss some of the incredible features Fastify has to offer. No configuration is necessary — Fastify’s code base will run on Node versions 6.x, 8.x, and 9.x.
Fastify版本1已于3月7日发布 。 这篇文章将向您展示如何进行设置,并且我们将讨论Fastify提供的一些令人难以置信的功能。 无需配置-Fastify的代码库将在Node版本6.x,8.x和9.x上运行。
Start with npm i fastify
and then:
从npm i fastify
开始,然后:
const fastify = require('fastify')()
fastify.get('/', (request, reply) => { reply.send({ hello: 'world' })})
fastify.listen(3000, err => { if (err) { fastify.log.error(err) process.exit(1) } fastify.log.info( `server listening on ${fastify.server.address().port}` )})
Now launch your server with: node server
现在使用以下命令启动服务器: node server
? That’s it! You’ve got your first Fastify server up and running.
? 而已! 您已经启动并运行了第一台Fastify服务器。
const fastify = require('fastify')()
Line 1 is importing the Fastify framework into the JavaScript project and instantiating it. Your server instance is now stored in the fastify
variable. You can pass additional options to this line like so:
第1行将Fastify框架导入JavaScript项目并实例化。 您的服务器实例现在存储在fastify
变量中。 您可以像这样将其他选项传递给此行:
const fastifyWithOptions = require('fastify')({ logger: { prettyPrint: true }})
Powered by the Pino logger, this option makes the console output easy to read and colorful. Check out the Pino documentation for more logger options, and the Fastify documentation for more Fastify instance options.
由Pino记录器提供动力,此选项使控制台输出易于阅读且色彩鲜艳。 请查看Pino文档以获取更多记录器选项,并查看Fastify文档以获取更多Fastify实例选项。
fastify.get('/', (request, reply) => { reply.send({ hello: 'world' })})
Lines 3 through 5 define a very basic Route. Routes are the core to any Node.js backend server. Fastify supports two methods of defining routes: the shorthand method used above, or a general .route
method as shown below.
第3至5行定义了一个非常基本的Route 。 路由是任何Node.js后端服务器的核心。 Fastify支持两种定义路由的方法:上面使用的速记方法,或如下所示的常规.route
方法。
fastify.route({ method: 'GET', url: '/', handler: function (request, reply) { reply.send({ hello: 'world' }) }})
Both of these implementations do the exact same thing and have the same performance, so simply use whichever makes the most sense to you.
这两种实现都做完全相同的事情,并且具有相同的性能,因此只需使用对您最有意义的方法即可。
Route declaration has many more options available that aren’t shown here.
路线声明还有更多可用选项,此处未显示。
Provide a JSON Schema for the request and response objects, which can increase throughput by 10–20%
为请求和响应对象提供JSON模式 ,这可以将吞吐量提高10–20%
Define a beforeHandler
method that is called just before the handler
function. This is great for authentication, and I demonstrate how to use it in my JWT Auth plugin (more on Fastify plugins later).
定义一个在handler
函数之前调用的beforeHandler
方法。 这对身份验证非常有用,我将演示如何在我的JWT Auth插件中使用它(稍后将在Fastify插件中进行介绍)。
fastify.listen(3000, err => { if (err) { fastify.log.error(err) process.exit(1) } fastify.log.info( `server listening on ${fastify.server.address().port}` )})
Finally, start the Fastify instance on localhost port 3000. This is the last step required to create your own Fastify instance. Internally this method will wait for .ready()
(which is called after loading plugins). No new routes can be defined after calling the .listen()
method.
最后,在本地主机端口3000上启动Fastify实例。这是创建自己的Fastify实例所需的最后一步。 在内部,此方法将等待.ready()
(在加载插件后调用)。 调用.listen()
方法后,无法定义新路由。
One of the best features of Fastify is how easy it is to write and incorporate plugins into a server instance. To start, define a function:
Fastify的最佳功能之一是编写插件并将其合并到服务器实例中是多么容易。 首先,定义一个函数:
function superPlugin (fastify, opts, next) { fastify.decorate('superMethod', () => { console.log(`Secret code: ${opts.secretCode}`) }) next()}
Now using the fastify-plugin
module, export your new plugin.
现在使用fastify-plugin
模块,导出新插件。
const fp = require('fastify-plugin')
module.exports = fp(superPlugin, { fastify: '>=1.0.0', name: 'super-plugin'})
Finally register your plugin onto your Fastify instance:
最后,将插件注册到Fastify实例上:
/* Inside the main server.js file */const superPlugin = require('super-plugin')
fastify.register(superPlugin, { secretCode: 'JavaScript is awesome!'})
Now you can call the superMethod
anywhere you have access to your Fastify instance.
现在,您可以拨打superMethod
您可以访问任何地方你Fastify实例。
/* server.js */
fastify.listen(3000, err => { fastify.superMethod()})
Just to note: you can register plugins within other plugins, which locks that child plugin’s scope to the parent plugin only. This topic is too advanced for this article, so I won’t be covering it any more detail. You can read more about Fastify plugins here. Check out the full example files in a Github gist here.
请注意:您可以在其他插件中注册插件,这会将子插件的作用域仅锁定到父插件。 该主题对本文而言太高级了,因此我将不再赘述。 您可以在此处阅读有关Fastify插件的更多信息。 在此处查看Github要点中的完整示例文件。
Fastify is fast. Really really fast ??
Fastify很快。 真的真的很快吗?
After this brief introduction, I encourage you to check out all that Fastify has to offer. If you enjoy open source programming, Fastify is a great project to contribute to as well. There is also a great ecosystem of plugins to check out and contribute to!
简短介绍之后,我建议您检查Fastify提供的所有内容 。 如果你喜欢开源编程,Fastify是一项伟大的工程,以促进为好。 还有一个很棒的插件生态系统可供签出并做出贡献!
Keep up the great work ~ Ethan Arrowood
继续努力吧〜Ethan Arrowood
Ethan Arrowood ??? (@ArrowoodTech) | TwitterThe latest Tweets from Ethan Arrowood ??? (@ArrowoodTech). always listening to music. probably contributing to open…twitter.com Ethan-Arrowood (Ethan Arrowood)Ethan-Arrowood has 80 repositories available. Follow their code on GitHub.github.com
伊桑·阿罗德(Ethan Arrowood) (@ArrowoodTech)| Twitter的 前作 从伊森夏洛特艾尔最新的鸣叫??? (@ArrowoodTech)。 总是听音乐。 可能有助于开放……twitte r.com Ethan-Arrowood(Ethan Arrowood) Ethan-Arrowood有80个存储库。 在GitHub上遵循他们的代码。 github.com
翻译自: https://www.freecodecamp.org/news/how-to-get-up-and-running-with-fastify-8b7e23781844/
fastify 后台