维纳斯和阿多尼斯故事_认识阿多尼斯! 适用于Node.js的Laravel样式MVC框架

维纳斯和阿多尼斯故事

Node.js is one of the emerging technologies to write real-time applications using one of your favorite web languages: Javascript. With the ample choices of frameworks to write your first web server, not even a single one offers the desired developer experience. This is where AdonisJs shines.

Node.js是使用您最喜欢的Web语言之一Javascript编写实时应用程序的新兴技术之一。 有了足够多的框架来编写您的第一个Web服务器,甚至没有一个可以提供所需的开发人员体验。 这就是AdonisJs闪耀的地方。

AdonisJs is a beautifully crafted MVC framework for Node.js. In this article I'll show you how to get started with AdonisJs by installing the framework, discussing the features and creating a welcome page.

AdonisJs是一个精巧的Node.js MVC框架 。 在本文中,我将向您展示如何通过安装框架,讨论功能并创建欢迎页面来开始使用AdonisJs。

什么是AdonisJs? ( What is AdonisJs? )

AdonisJs is inspired by a Php Framework called Laravel. It borrows the concepts of Dependency injection and service providers to write beautiful code which is testable to its core.

AdonisJs受到名为Laravel的Php框架的启发 。 它借用了依赖注入和服务提供者的概念来编写可对其核心进行测试的漂亮代码。

In the age of shiny web frameworks, AdonisJs focuses on the key aspects of creating stable and scalable web applications, some of them are:

在闪亮的Web框架时代,AdonisJs专注于创建稳定且可扩展的Web应用程序的关键方面,其中一些是:

开发者经验 (Developer Experience)

The framework makes use of latest inbuilt ES2015 features to get rid of spaghetti code. You will hardly find yourself writing callbacks since there is a solid support for ES2015 generators. Also, the OOP nature of the framework helps you in abstracting your code into multiple re-usable chunks.

该框架利用最新的内置ES2015功能来摆脱意大利面条式代码。 您几乎不会发现自己编写回调,因为对ES2015生成器提供了可靠的支持。 同样,框架的OOP性质可帮助您将代码抽象为多个可重用的块。

一致的API (Consistent API)

The API throughout the code base is so consistent that after working for a while, you will be able to guess the method names, expected output, etc.

整个代码库中的API都是一致的,经过一段时间后,您将能够猜出方法名称,预期输出等。

速度与生产力 (Speed & Productivity)

AdonisJs ships with a bunch of 1st party components known as providers. Writing entire web server is a matter of weeks(if not days). It ships with solid support for Mailing, Authentication, Redis, SQL ORM, Data validation and sanitization, etc.

AdonisJs附带了许多称为提供程序的第一方组件。 编写整个Web服务器大约需要数周(如果不是几天的话)。 它附带了对MailingAuthenticationRedisSQL ORMData验证和清理等的可靠支持。

安装AdonisJs ( Installing AdonisJs )

The installation process is straight forward. Just make sure you have got versions of node >= 4.0 and npm >=3.0.

安装过程很简单。 只要确保您具有的node >= 4.0npm >=3.0

Let's start by installing adonis-cli globally using npm. It is a command line tool to scaffold new applications within a matter of seconds.

让我们从使用npm全局安装adonis-cli开始。 它是一个命令行工具,可以在几秒钟内支持新的应用程序。

npm i -g adonis-cli

install-cli.gif

Now we can start by creating new projects. For this article, we will setup a project called hello-adonis.

现在我们可以开始创建新项目。 对于本文,我们将设置一个名为hello-adonis的项目。

adonis new hello-adonis

Above command will setup a new project with the pre-configured directory structure. Don't get overwhelmed by the directory structure as in next article we will talk about it.

上面的命令将使用预配置的目录结构设置一个新项目。 不要被目录结构淹没,因为在下一篇文章中我们将讨论它。

Now cd into the newly configured project and executing the following command to start the HTTP server.

现在,使用cd进入新配置的项目,并执行以下命令来启动HTTP服务器。

cd hello-adonis
npm start

# info adonis:framework +6ms serving app on localhost:3333

Open http://localhost:3333 to see the welcome page.

打开http:// localhost:3333以查看欢迎页面。

In this article, we will discuss the framework concepts and some exciting features and in the next article, we will have fun by writing some code.

在本文中,我们将讨论框架概念和一些令人兴奋的功能,在下一篇文章中,我们将通过编写一些代码来获得乐趣。

强大的路由 ( Powerful Routing )

AdonisJs has out of the box support for defining fluent routes. Setting up express style routes to CRUD resources is a just a matter of seconds.

AdonisJs具有开箱即用的支持,用于定义流畅的路线。 设置到CRUD资源的快速样式路由仅需几秒钟。

Let's explore the framework routing layer.

让我们探索框架路由层。

const Route = use('Route') // importing route

Route.get('/home', 'HomeController.welcome')

Above we defined a simple route to URL /home. Once the given route is called the welcome method on HomeController will be invoked.

上面我们定义了一个简单的URL /home路由。 调用给定的路由后,将调用HomeController上的welcome方法。

Let's move to my favorite part of routing called resources. Resources help you in defining multiple conventional RESTful routes within a single line of code.

让我们转到我最喜欢的路由部分,称为resources 。 资源可帮助您在一行代码中定义多个常规RESTful路由。

const Route = use('Route') // importing route

Route.resource('users', 'UserController')

Above line of code will set a total of seven routes.

上面的代码行将设置总共7条路线。

UrlVerbController MethodPurpose
/usersGETindexShow list of all users
/users/createGETcreateDisplay a form to create a new user.
/usersPOSTstoreSave user submitted via form to the database.
/users/:idGETshowDisplay user details using the id
/users/:id/editGETeditDisplay the form to edit the user.
/users/:idPUT/PATCHupdateUpdate details for a given user with id.
/users/:idDELETEdestroyDelete a given user with id.
网址 动词 控制器方式 目的
/用户 得到 指数 显示所有用户的列表
/用户/创建 得到 创造 显示表单以创建新用户。
/用户 开机自检 商店 将通过表单提交的用户保存到数据库中。
/ users /:id 得到 表演 使用ID显示用户详细信息
/ users /:id / edit 得到 编辑 显示表单以编辑用户。
/ users /:id PUT / PATCH 更新 更新具有ID的给定用户的详细信息。
/ users /:id 删除 破坏 删除具有ID的给定用户。

That's not all. You can also filter and extend resources based upon the nature of the application.

那不是全部。 您还可以根据应用程序的性质来过滤和扩展资源。

const Route = use('Route') // importing route

Route
    .resource('users', 'UserController')
    .only(['index', 'store', 'update', 'destroy'])
    .addMember('profile')

Whoooo! As I said, the fluent method chaining makes it so simple to filter the resources down to only four routes, and also with the help of addMember, we can add a new route to the users resource.

!! 就像我说的那样,流利的方法链接使将资源过滤到仅四个路由变得如此简单,并且借助addMember ,我们可以向users资源添加新的路由。

Here's a neat video to show off some more routing features:

这是一个简洁的视频,展示了更多的路由功能:

路由中间件 (Route Middleware)

Before we move onto the next feature, let's see how to work with route middleware.

在介绍下一个功能之前,让我们看一下如何使用路由中间件。

Route
    . get ( '/account' , 'UserController.account' )
    . middleware ( 'auth' )

Middleware(s) are defined by chaining the middleware method, and you are free to specify one or more middleware by passing multiple arguments.

中间件是通过链接middleware方法定义的,您可以通过传递多个参数来自由指定一个或多个中间件。

活动记录ORM ( Active Record ORM )

If you ever worked with Rails or Laravel, you will feel right at home. AdonisJs ORM called Lucid is a robust implementation of Active Record. It supports all popular SQL databases like MYSQL, Oracle, PostgreSQL, SQLite, etc. Also, the database engine has support for data models, fluent query builder, and migrations.

如果您曾经与Rails或Laravel一起工作过,您会感到宾至如归。 AdonisJs称为Lucid的 ORM是Active Record的可靠实现。 它支持所有流行SQL数据库,例如MYSQLOraclePostgreSQLSQLite等。此外,数据库引擎还支持数据模型,流畅的查询生成器和迁移。

Let's take a quick look at how to define and interact with SQL data models also known as Lucid models.

让我们快速看一下如何定义SQL数据模型并与之交互(也称为Lucid模型)

const Lucid = use ( 'Lucid' )

class User extends Lucid {

    posts ( ) {
        return this . hasMany ( 'App/Model/Post' )
    }

}

Now you can use the Post Model as follows.

现在,您可以按以下方式使用发布模型。

const User = use ( 'App/Model/User' ) // require user model

yield User . all ( ) // get all users
const loue = yield User . findBy ( 'username' , 'Loue' ) // find by username

yield loue . posts ( ) . fetch ( ) // fetch all posts by loue

Ace命令 ( Ace Commands )

There's hardly any Node.js framework which gives you the power to write project-specific terminal commands with the ease as similar to AdonisJs. AdonisJs has a beautiful tool called Ace, which makes it super easy and intuitive to write your terminal commands. Don't believe me! Let's check it out.

几乎没有任何Node.js框架可以像AdonisJs一样轻松地编写特定于项目的终端命令。 AdonisJs有一个漂亮的工具叫做Ace ,它使编写终端命令变得非常容易和直观。 不相信我! 让我们来看看。

Commands are stored in the app/Commands directory, and each file represents a single command. Let's play with the pre-existing Greet command.

命令存储在app/Commands目录中,每个文件代表一个命令。 让我们玩一下预先存在的Greet命令。

Run the following command from the root of your project.

从项目的根目录运行以下命令。

./ace greet "Your name."

It is the simplest command you can ever write, but that does not limit you from creating more useful commands. For now, we will discuss the basic structure of a command class. Whereas you must reference the offical documentation.

这是您可以编写的最简单的命令,但这并不限制您创建更多有用的命令。 现在,我们将讨论命令类的基本结构。 而您必须参考官方文档

const Command = use ( 'Command' )

class Greet extends Command {

    get signature ( ) {
        return 'greet {name}'
    }

    get description ( ) {
        return 'Greet a user with their name.'
    }

    * handle ( args , options ) {
        // called when command is executed
    }

}
  1. Command signature defines the command name and the required/optional arguments to be passed when running the command.

    命令signature定义命令名称和运行命令时要传递的必需/可选参数。
  2. The description gets displayed on the help screen.

    说明显示在帮助屏幕上。
  3. handle is an ES2015 generator method, which is called when your command is invoked. You are free to do almost anything inside this method.

    handle是ES2015生成器方法,在调用命令时会调用该方法。 您可以在此方法内执行几乎所有操作。

功能一览 ( Features At A Glance )

There is a lot more to cover when it comes to AdonisJs features. Let's sum them within a small list to keep this first article simple and easy to digest.

关于AdonisJs的功能,还有很多要讲的内容。 让我们将它们汇总在一个小列表中,以使第一篇文章简单易懂。

  1. Multi-transport mailer.

    多路运输邮件。
  2. Robust middleware layer to interact with incoming HTTP requests.

    强大的中间件层可与传入的HTTP请求进行交互。
  3. Nunjucks based templates.

    基于Nunjucks的模板。
  4. Support for emitting and listening to application-wide events.

    支持发出和监听应用程序范围的事件。
  5. Inbuilt support for Redis.

    对Redis的内置支持。
  6. Secure and straightforward file uploads.

    安全,简单的文件上传。
  7. Security features with support for CORS, CSRF, Content sniffing and XSS attacks.

    支持CORSCSRF内容嗅探XSS攻击的安全功能。
  8. Supportive and friendly community.

    支持和友好的社区。

未来 ( Future )

AdonisJs is under active development with the latest release of version 3.0. Major API specs have finalized so that upcoming releases will have less or no breaking changes.

AdonisJs正在积极开发最新版本的3.0版。 主要的API规范已经完成,因此即将发布的版本将有很少或没有重大更改。

Also, there is a complete roadmap on Trello sharing what's next in the queue. To summarize, you will soon get support for:

另外,在Trello上有完整的路线图,共享队列中的下一步。 总而言之,您很快就会获得以下支持:

  1. Social Authentication via Facebook, Google, Github, etc.

    通过Facebook,Google,Github等进行的社交身份验证。
  2. Complete testing framework.

    完整的测试框架。
  3. Support for caching database queries and views fragments.

    支持缓存数据库查询和视图片段。
  4. Inbuilt support for posting messages to Slack.

    对将消息发布到Slack的内置支持。

接下来的是! ( Coming up Next! )

This post is an introduction to AdonisJs, walking through some of the fundamentals and key features of the framework. In the upcoming post, we will together write a simple link sharing website like EchoJs.

这篇文章是对AdonisJs的介绍,并介绍了该框架的一些基础知识和关键功能。 在即将发布的帖子中,我们将共同编写一个简单的链接共享网站,例如EchoJs

翻译自: https://scotch.io/tutorials/meet-adonisjs-a-laravel-style-mvc-framework-for-node-js

维纳斯和阿多尼斯故事

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值