Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Trong đợt vừa rồi, mình có cần tìm một framework NodeJS cho dự án. Sau một vòng tìm hiểu mình đã tìm thấy Adonis, một framework NodeJS hoàn thiện được tổ chức theo phong cách giống hệt Laravel!!
AdonisJs là một Nodejs Framework được viết lại đựa trên mô hình MVC và được thiết kế khá giống với Laravel Framework, giống về cấu trúc thư mục và cách viết. Vì thế với những bạn nào đã sữ dụng qua Laravel thì khi tiếp xúc với AdonisJs các bạn sẽ khá dẽ dàng nắm bắt.
Hiện tại trên github, adonis đang có 5777 sao (tại thời điểm viết bài), một lượng sao cũng khá lớn với tuổi đời > 3 năm.
-Tương tác mạnh mẽ trong cơ sở dữ liệu
-Api và cơ chế xác thực
-Dẽ dàng gữi mail với SMTP hoặc các dịch vụ khác
-Cho phép validate input đầu vào
-Bảo mật cao
-Dễ dàng mở rộng
Nếu bạn nào từng làm PHP - Laravel thì sẽ thấy cấu trúc thư mục gần như giống hệt, chỉ khác chút xíu ở thư mục start.
Adonis cũng bao gồm Routing, Middleware, Controllers, Request, Response, Views, Validator, Models, Command, ORM, Config, Env v.v...
Đó là phần vỏ, giờ chúng ta sẽ xem đến phần ruột
Mình ví dụ một model cơ bản:
'use strict'
const Model = use('Model')
class Post extends Model {
author () {
return this.belongsTo('App/Models/User', 'user_id')
}
}
module.exports = Post
Vẫn có model và relation hasOne, hasMany, belongsTo v.v...
class UserController {
async store ({ auth, session, request, response }) {
/**
* Getting needed parameters.
*
* ref: http://adonisjs.com/docs/4.1/request#_only
*/
const data = request.only(['username', 'email', 'password', 'password_confirmation'])
/**
* Validating our data.
*
* ref: http://adonisjs.com/docs/4.1/validator
*/
const validation = await validateAll(data, {
username: 'required|unique:users',
email: 'required|email|unique:users',
password: 'required',
password_confirmation: 'required_if:password|same:password',
})
/**
* If validation fails, early returns with validation message.
*/
if (validation.fails()) {
session
.withErrors(validation.messages())
.flashExcept(['password'])
return response.redirect('back')
}
// Deleting the confirmation field since we don't
// want to save it
delete data.password_confirmation
/**
* Creating a new user into the database.
*
* ref: http://adonisjs.com/docs/4.1/lucid#_create
*/
const user = await User.create(data)
// Authenticate the user
await auth.login(user)
return response.redirect('/')
}
}
module.exports = UserController
class RedirectIfAuthenticated {
async handle ({ auth, request, response }, next) {
/**
* Verify if we are logged in.
*
* ref: http://adonisjs.com/docs/4.0/authentication#_check
*/
try {
await auth.check()
return response.redirect('/')
} catch (e) {}
await next()
}
}
module.exports = RedirectIfAuthenticated
Trên đây mình chỉ đưa ra 1 vài demo để mọi người hiểu qua framework và sự tương đồng của adonis và laravel. Tài liệu chi tiết các bạn có thể tham khảo ở https://adonisjs.com/docs/4.1/installation
Vì vậy nếu ai đã quen thuộc với laravel thì khi cần làm NodeJS có thể lựa chọn Adonisjs.