基於MySql和Sails.js的RESTful風格的api實現

來源:互聯網
上載者:User

標籤:red   風格   rate   plt   tor   版本   實現   engine   資料   

Sails.js是類似於express的node後台架構,她面向資料庫的特性使得面向資料庫的伺服器的搭建變得特別簡單快捷。

現在網上關於Sails的教程大多是基於V0.12版本的,但是現在Sails的最新版本已經是V1.0,對照著官方文檔研究了一下,總結了一下基於MySql和Sails的最簡單實現。

 

Step1:

1.安裝Sails,建立一個Sails App,安裝用於Sails的MySql組件

1 npm install sails -g2 sails new myApp3 npm install sails-mysql --save

2.建立測試資料表

1 CREATE TABLE `test` (2   `id` int(11) NOT NULL AUTO_INCREMENT,3   `username` varchar(255) DEFAULT NULL,4   `password` varchar(255) DEFAULT NULL,5   PRIMARY KEY (`id`)6 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Step2:

1.修改config/datastore為

1 // config/datastores.js2 module.exports.datastores = {3   default: {4     adapter: require(‘sails-mysql‘),5     url: ‘user:[email protected]:port/database‘,6   }7 };

2.修改config/models為

1 // config/models.js
2
moudle.exports.models = {3 migrate: ‘safe‘,4 dataEncryptionKeys: {5 default: ‘k+07rP56MgaS5L4PIbpGnPltb+aO0O0PD8Fh8Upqzvc=‘6 },7 cascadeOnDestroy: true,8 primaryKey: ‘id‘9 };

Step3:

建立api

1 sails generate api test

Step4:

將資料表欄位填入api/models/Test

1 // api/models/Test.js2 module.exports = {3   attributes: {4     id: { type: ‘number‘, required: true,autoIncrement: true },5     username: { type: ‘string‘, required: true },6     password: { type: ‘string‘, required: true },7   },8 };

啟動app,大功告成。

基於MySql和Sails.js的RESTful風格的api實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.