Viết api nodejs băng Sails Framework (P1)
Bài đăng này đã không được cập nhật trong 8 năm
Tổng quan
Express là mini framework Nodejs phổ biến và tiện lợi. Chúng ta có thể viết bất cứ ứng dụng nào trên Express. Tuy nhiên, cấu trúc ứng dụng Express là đơn giản không theo một design pattern nào. Nếu bạn từng làm PHP thì chắc hẳn sẽ ấn tượng và yêu thích với PHP framework phổ biến nhất hiện nay, Laravel. Với cấu trúc rõ ràng, hỗ trợ tối ưu cho developer phát triển ứng dụng nhanh nhất, tốt nhất. Sailsjs framework được ví như Laravel trên nên tảng Nodejs, được thiết kế theo mô hình MVC.
Xây dựng Nodejs api bằng Sails
Yêu cầu
- Môi trường của bạn đã được cài đặt
Nodejsvànpm - Nếu chưa cài đặt Nodejs bạn có thể tham khảo tại đây
Cài đặt Sails
sudo npm install -g sails
Ứng dụng đầu tiên
Tạo ứng dụng
sails new test-project
Khởi tạo server môi trường development.
cd test-projectsails liftMở trình duyệt : http://localhost:1337 để thấy homepage.
Cấu trúc ứng dụng Sails
- api
- app.js
- assets
- config
- Gruntfile.js
- node_modules
- package.json
- README.md
- tasks
- views
Trong đó:
apilà thư mục chưa toàn bộ core của api bao gồmcontrollers,models,policies,responses,services… Controllers và models sẽ được khởi tạo khi chạy lệnh:sails generate api users
- controllers/
--- UserController.js
- models/
--- User.js
- policies/
--- sessionAuth.js
- responses/
--- badRequest.js
--- forbidden.js
--- notFound.js
- services/
assetslà thư mục chứa ảnh, js, style css, template ..
- assets
--- images
--- js
--- styles
--- templates
configlà thư mục chứa tất cả các config của ứng dụng như biến môi trườngENV,routes,session…taskslà thư mục chứa các tasks để sync, copy, minify nhưuglify,concat,cssmin…viewslà thư mục chưa toàn bộ view response như403.ejs,500.ejs,layout.ejsappjschính là linh hồn của ứng dụng, là file core của mọi ứng dụng Nodejs.
Xây dựng REST API đơn giản
Xây dựng api user và profile
Create controller and model
sails generate api usersails generate api profile
Sails tự động tạo một controller và model
Model
Mở user model tại api/models/User.js và config như sau:
module.exports = {
attributes: {
username:{
type: 'string'
},
email:{
type: 'string'
},
password:{
type: 'string'
}
}
};
Sau đó thêm thuộc tính cho profile model, chúng ta thêm thuộc tính userid như là một foreignKey của user model.
module.exports = {
attributes: {
userid:{
model: 'user'
},
firstname:{
type: 'string'
},
lastname:{
type: 'string'
},
gender:{
type: 'string'
},
dob:{
type: 'date'
},
address:{
type: 'text'
}
}
};
Tạo user mới:
-
sails lift -
Mở trình duyệt tạo mới user
http://localhost:1337/user/create?username=cendekiapp&password=12345&email=me@cendekiapp.com -
Kết quả trả về ở dạng json
{
"username": "cendekiapp",
"password": "12345",
"email": "me@cendekiapp.com",
"createdAt": "2017-02-02T23:13:22.995Z",
"updatedAt": "2017-02-02T23:13:22.995Z",
"id": 1
}
Tạo profile
Tạo profile cho user có id = 1 ở trên.
http://localhost:1337/profile/create?userid=1&firstname=Cendekia&lastname=Putra&gender=male&dob=1987-01-01&address=loremipsum
{
"username": "cendekiapp",
"password": "12345",
"email": "me@cendekiapp.com",
"createdAt": "2017-02-02T23:13:22.995Z",
"updatedAt": "2017-02-02T23:13:22.995Z",
"id": 1
}
Lấy danh sách user
http://localhost:1337/user
[
{
"username": "cendekiapp",
"password": "12345",
"email": "me@cendekiapp.com",
"createdAt": "2017-02-02T23:13:22.995Z",
"updatedAt": "2017-02-02T23:13:22.995Z",
"id": 1
}
]
Lấy danh sách profile
http://localhost:1337/profile
[
{
"userid": {
"username": "cendekiapp",
"password": "12345",
"email": "me@cendekiapp.com",
"createdAt": "2017-02-02T23:13:22.995Z",
"updatedAt": "2017-02-02T23:13:22.995Z",
"id": 1
},
"firstname": "Cendekia",
"lastname": "Putra",
"gender": "male",
"dob": "1987-01-01T00:00:00.000Z",
"address": "loremipsum",
"createdAt": "2017-02-02T23:20:57.131Z",
"updatedAt": "2017-02-02T23:20:57.131Z",
"id": 1
}
]
Update profile
Update profile id = 1
http://localhost:1337/profile/update/1?lastname=pramana
{
"userid": {
"username": "cendekiapp",
"password": "12345",
"email": "me@cendekiapp.com",
"createdAt": "2017-02-02T23:13:22.995Z",
"updatedAt": "2017-02-02T23:13:22.995Z",
"id": 1
},
"firstname": "Cendekia",
"lastname": "pramana",
"gender": "male",
"dob": "1987-01-01T00:00:00.000Z",
"address": "loremipsum",
"createdAt": "2017-02-02T23:20:57.131Z",
"updatedAt": "2017-02-02T23:23:19.378Z",
"id": 1
}
Xóa profile
http://localhost:1337/profile/destroy/1
Kết luận
Như vậy chúng ta đã tạo thành công REST API cơ bản và nhanh chóng bằng Sails framework. Phần tới mình sẽ trình bày sâu hơn về các phần: tương tác với database mysql, authenticate api. Download source tại đây.
All rights reserved