oracle cdr是什么,CDRD TALK|全栈架构Sails.js简介

article/2025/9/14 4:13:47

原标题:CDRD TALK|全栈架构Sails.js简介

Sails.js是一个可伸缩的、数据驱动的、面向服务的现代App架构。它致力于构建基于Node.js服务的定制化企业级应用。在Sails.js之前,构建一个实用的产品级Node.js应用的时间成本通常以月为单位计算。但是使用Sails.js后,只需要数周,便可以完成这一切。下面我们一步一步的建立一个基于Sails.js的示例App

环境搭建

打开一个命令行窗口,依次执行下列的命令

安装Node.js:https://nodejs.org/en/

安装Sails.js:

npm install sails -g

新建一个Sails.js的App

sails new test-project

然后你会看到

info: Created a new Sails app `test-project`!

意味着你已经成功创建了一个Sails.js的应用

执行

cd test-project

sails lift

然后你就可以打开浏览器访问localhost:1337,看到Sails.js的欢迎界面应用结构

Sails应用的结构与一般的node.js应用目录一致,比较特殊的目录如下:

api

assets

config

tasks

views

api目录实现了一整套的MVC模式的后台接口。

assets目录存放项目所有的静态资源,包括图片,js文件,样式文件,前端模板文件等

config目录存放项目的配置文件,包括项目构建时的环境变量,部署时的语言版本,以及一些session和路由相关的配置

tasks目录存放项目构建时的各种任务,比如打包js文件;监听项目文件的热部署;注入静态资源等。

views目录存放项目的所有界面

重要概念 Middleware(中间件)

中间件概念是node.js应用的一个重要特点。Sails.js使用了一个额外的可配置的中间件栈,这样当服务器收到一个http的请求时,配置好的中间件栈为为这个请求依次执行。

中间件定义在config/http.js:

// ...middleware: { // Define a custom HTTP middleware fn with the key `foobar`:foobar: function(req,res,next){ /*...*/next(); }, // Define another couple of custom HTTP middleware fns with keys `passportInit` and `passportSession`// (notice that this time we're using an existing middleware library from npm)passportInit : require( 'passport').initialize(), passportSession : require( 'passport').session(), // Override the conventional cookie parser:cookieParser: function(req, res, next){ /*...*/next(); }, // Now configure the order/arrangement of our HTTP middlewareorder: [ 'startRequestTimer', 'cookieParser', 'session', 'passportInit', // <==== passport HTTP middleware should run after "session"'passportSession', // <==== (see https://github.com/jaredhanson/passport#middleware)'bodyParser', 'compress', 'foobar', // <==== we can put this stuff wherever we want'methodOverride', 'poweredBy', '$custom', 'router', 'www', 'favicon', '404', '500'] }, customMiddleware: function(app){ //Intended for other middleware that doesn't follow 'app.use(middleware)' conventionrequire( 'other-middleware').initialize(app); } // ...Models and ORM

Sails.js默认安装Waterline,这是一个强大的SQL/noSQL的数据映射引擎(ORM/ODM)。Waterline在数据库之上抽象了一个操作层,可以屏蔽掉底层的数据库操作,显著的简化一个或者多个数据库的交互。

比如使用了表结构的Oracle和Mysql;使用了集合概念的MongoDB;使用了key/value键值对的Redis。它们都有自己的一套操作语言,如果你的项目需要在这几中数据库之间迁移,或者你需要使用多个数据库,那你需要针对不同的数据库编写不同的操作语句,极大的浪费了资源。

你需要做的仅仅是在config/connecttions.js中配置:

// ...connections:{ local_mysql:{ //arbitrary namemodule: 'sails-mysql', user: 'root', password: 'root', url: 'mysql://root:root@localhost:3306/sailstest1'}} // ...Sessions

在Sails.js中,sessions是用来在不同请求之间存储客户端信息的。由于http协议是无状态的协议,所以无法保持客户端的状态.通过sessions,我们可以通过请求中的特定参数,确定是否是来自于统一个客户端的请求,从而达到客户端状态保持效果。

Sails.js的sessions主要由三个组件实现:

session存储,可以是默认的Sails session store,或者数据库。

session管理,Sails.js通过中间件管理session。

请求中的cookie,Sails.js在每次前端发送请求时,都会在cookie添加一个特别的标志位(默认是sails.sid)。

在api/controller下,我们可以新建一个js如下:

module.exports = {login: function(req, res){ // Authentication code here// If successfully authenticatedreq.session.userId = foundUser.id; // returned from a databasereturnres.json(foundUser);}}

它只包含一个login的action方法,如上所示,我们在Sails.js中,我们可以直接通过request访问当前客户端的session。Policies

Sails.js通过policies来实现认证和访问控制相关功能。你可以在api/policies中,配置如下:

// policies/canWrite.jsmodule.exports = functioncanWrite(req, res, next){ vartargetFolderId = req.param( 'id'); // If the requesting user is not logged in, then they are _never_ allowed to write.// No reason to continue-- we can go ahead and bail out now.if(!req.session.me) { returnres.redirect( '/login'); } // Check the database to see if a permission record exists which matches both the// target folder id, the appropriate "type", and the id of the logged-in user.Permission.findOne({ folder: targetFolderId, user: req.session.me, type: 'write'}) .exec( function(err, permission){ // Unexpected error occurred-- use the app's default error (500) handler. > We do this because this should never happen, and if it does, it means there// > is probably something wrong with our database, and we want to know about it!)if(err) { returnres.serverError(err); } // No "write" permission record exists linking this user to this folder.// Maybe they got removed from it? Or maybe they never had permission in the first place...if(!permission) { returnres.redirect( '/login'); } // If we made it all the way down here, looks like everything's ok, so we'll let the user through.// (from here, the next policy or the controller action will run)returnnext(); });}; 接口示例

现在,我们要实际测试一下Sails.js整个数据流程。

1. UserController

我们在api/controllers目录下新建UserController.js文件,如下:

/** * UserController * * @deion :: Server-side logic for managing users * @help :: See http://sailsjs.com/documentation/concepts/Controllers */module.exports = {}; 2. User

我们在api/models目录下新建User.js如下:

/** * User.js * * @deion :: TODO: You might write a short summary of how this model works and what it represents here. * @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models */module.exports = { attributes: { name: {type: "string"}, age: {type: "integer"} }}; 3. 测试

首先,我们访问一下http://localhost:1337/user

[]

/user接口,表示获取当前用户列表。

然后,我们访问一下http://localhost:1337/user/create?name=abby&age=24

{

“name”: “abby”,

“age”: 24,

“createdAt”: “2017-10-16T10:16:51.154Z”,

“updatedAt”: “2017-10-16T10:16:51.154Z”,

“id”: 2

}

可以看到已经创建了一个名叫abby的用户

这个时候我们在创建一个用户:http://localhost:1337/user/create?name=connor&age=28

然后访问http://localhost:1337/user

[

{

“createdAt”: “2017-10-16T10:14:55.028Z”,

“updatedAt”: “2017-10-16T10:16:31.173Z”,

“id”: 1,

“name”: “connor”,

“age”: 28

},

{

“name”: “abby”,

“age”: 24,

“createdAt”: “2017-10-16T10:16:51.154Z”,

“updatedAt”: “2017-10-16T10:16:51.154Z”,

“id”: 2

}

]

接着,我们修改connor用户:http://localhost:1337/user/update/1?name=connor123&age=30

然后查看用户列表http://localhost:1337/user:

[

{

“createdAt”: “2017-10-16T10:14:55.028Z”,

“updatedAt”: “2017-10-16T10:20:28.337Z”,

“id”: 1,

“name”: “connor123”,

“age”: 30

},

{

“name”: “abby”,

“age”: 24,

“createdAt”: “2017-10-16T10:16:51.154Z”,

“updatedAt”: “2017-10-16T10:16:51.154Z”,

“id”: 2

}

]

最后我们删除abby用户http://localhost:1337/user/destroy/2

[

{

“createdAt”: “2017-10-16T10:14:55.028Z”,

“updatedAt”: “2017-10-16T10:20:28.337Z”,

“id”: 1,

“name”: “connor123”,

“age”: 30

}

]

可以看到,当我们创建一个User的controller和model的时候,Sails.js自动为我们实现关于这个user的增删改查接口,这在很多时候可以节省很多的开发时间。

服务端界面渲染

很多时候,我们需要服务端直接渲染好界面返回前端,而不是返回一些数据。下面我们一步一步实现服务端的渲染

1. 配置路由 // config/routes.jsmodule.exports.routes = { '/': { view: 'homepage'}, 'get /renderUser': 'UserController.renderUser'} 2. 修改UserController.js // api/UserController.jsmodule.exports = { renderUser: function(req, res){ varparams = _.extend(req.query || {}, req.params || {}, req.body || {}); varid = params.id; User.findAll().exec( function(err, users){ res.view( 'manage/renderUser', {users: users}); }); }} 3. 用户列表界面

姓名年龄

然后我们访问:http://localhost:1337/renderUser:

姓名 年龄

connor 28

访问控制

目前来说,访问控制通常是根据用户请求时的cookie中的某个标志位来校验。这里我们简单实现一下拦截用户请求,校验后返回对应的结果。修改代码如下:

// api/policies/sessionAuth.jsmodule.exports = function(req, res, next){ // User is allowed, proceed to the next policy,// or if this is the last policy, the controllerif(req.cookies.authenticated === '1') { returnnext(); } // User is not allowed// (default res.forbidden() behavior can be overridden in `config/403.js`)returnres.forbidden( 'You are not permitted to perform this action.');}; module.exports.policies = { // config/policies.js'*': true, 'UserController': { '*': false, 'renderUser': 'sessionAuth'}}

可以看到我们取request带有cookies里面的authenticated属性,如果属性为1,那么可以访问网站,否则抛出错误。然后,我们访问http://localhost:1337/renderUser:网站会抛出一个403(Forbidden)错误。这个时候,我们在cookie中加入authenticated = 1,如下:

85f9be34a1bc5e7c904d559e48dceadc.png

然后再访问http://localhost:1337/renderUser,可以发现已经可以正常访问了。

数据库的配置

以上,我们完成了一个网站从前端到后台的业务层面的相关代码。但是我们还有一个很重要的地方没有涉及,那就是我们的数据库。我们先看看Sails.js默认的数据存储:

// config/connections.jsmodule.exports.connections = { localDiskDb: { adapter: 'sails-disk'}}

这里的sails-disk是sails默认的存放数据的方式,我们可以在项目根目录的.temp/localDiskDb.db文件中看到:

{ " data": { "user": [ { "name": "connor", "age": 28, "createdAt": "2017-10-17T05:48:47.682Z", "updatedAt": "2017-10-17T05:48:47.682Z", "id": 11} ]}, " schema": { "user": { "name": { "type": "string"}, "age": { "type": "integer"}, "id": { "type": "integer", "autoIncrement": true, "primaryKey": true, "unique": true}, "createdAt": { "type": "datetime"}, "updatedAt": { "type": "datetime"}}}, " counters": { "user": { "id": 11}}}

可以看到,我们之前定义的user的表,以及一条connor的用户数据。但是,在真实的产品级服务中,我们不可能使用这种方式来存储数据,下面我们实现一个Sails.js把数据存储到mysql中的示例。

1. 安装Mysql

前往 mysql 下载地址,下载免费的Mysql社区版本,并且按照安装提示,安装好mysql。之后,建立一个测试用的sails-test数据库,并建一个user表:包含name和age两个属性。

2. Sails.js代码 // config/connections.jsmodule.exports.connections = { someMysqlServer: { adapter: 'sails-mysql', host: '127.0.0.1', user: 'root', //optionalpassword: '123456', //optionaldatabase: 'sails_test'//optional},} // config/models.jsmodule.exports.models = { connection: 'someMysqlServer', migrate: 'alter'};

然后,我们访问http://localhost:1337/user/create?name=abby&age=24创建一个abby用户。登录mysql,查看use表:

可以看到abby这条数据确实插入了mysql数据库中。

结语

至此,我们已经使用Sails.js完成了一整套的网站业务。可以发现,由于我们使用了Sails.js这个架构,给我们带来了极大的方便,各种基础业务:数据库链接、前端路由、后台渲染、访问控制等,都已经封装得比较完善。我们只需要关注业务逻辑,无需关心底层实现。当然,Sails.js在我写作这篇文章时已经是过时的架构,但是我们依然可以吸取这种全栈架构的思路,如果对全栈架构有兴趣,可以到meteor这个地方去看看最新最火的Java全栈架构。返回搜狐,查看更多

责任编辑:


http://chatgpt.dhexx.cn/article/cj56sMJ9.shtml

相关文章

node-sails后台搭建

这个就直接简单搭建最基本的后台了 一、安装 安装sails npm i sails sails -v //检测版本 创建空项目 sails new my-app 安装数据库 cd my-app npm install sails-mysql -save 二、文件配置 Datastores.js 里面的数据库配置url Local.js里面port :1448端口 服务启动的端口 …

三、以user表为例,用Amis+Sails实现增删改查操作

文章目录 CRUD 组件查查询api分页fetcher参数观察统一处理method分页参数提交到后端自定义分页和页面大小&#xff08;pageSize&#xff09; 搜索排序头部工具条列折叠按钮刷新和导出excel自定义内容 删单条删除批量删除 增新增数据headerToolbar 结果分析前端数据格式要求 改数…

三、Sails 中使用Jwt进行身份认证

文章目录 Jwt 概述为什么要用JwtJwt原理 Jwt认证安装 Jwt 库登录ApiVerify Signature过期时间Nodejs 单线程易崩问题 验证程序修改配置积极策略消极策略多重验证 Jwt 测试正常登录过期或错误密钥测试 Jwt 概述 由于我们是完全前后端分离的开发模式&#xff0c;我们的后端对前端…

Sails基础之Controller层

通过前面的使用&#xff0c;我们可以看出Sails中MVC的结构更倾向于MVP的概念&#xff0c;Presenter在Sails中被称之为Actions&#xff1a; They often act as a middleman between your models and views. Controller层这个结构上的变化是Sails v1.0中新提出的方案&#xff0c;…

二、 在Sails中使用Typescript

文章目录 Typescript 基础Typescript 安装TypeScript 问题最简单的改造 Sails重定义Waterline&#xff08;Orm&#xff09; 重写ModelsTypescript 重写控制器User Model的进一步优化前后端约定 路径别名tsconfig.jsonmodule-alias安装使用 Jest测试 Typescript 基础 Typescrip…

四、Sails项目的Api文档——集成Swagger解决方案

文章目录 Api的迷思SwaggerSwagger概述在Sails中集成Swagger安装Swagger 生成设置生成的内容SwaggerUI Assets和.tmpBlueprint 蓝图Blueprint是什么Blueprint 配置local.js 进一步控制Swagger输出路由过滤路由的Swagger配置进一步优化Authorization Api的迷思 我们都知道写代码…

sails mysql_Sails+MVC+Mysql+Node+学习笔记一

项目构建 安装Node就不多说了&#xff0c; 1.sails安装与项目新建运行 npm install sails -g//全局安装 sails new project-name//新建项目 cd project-name //进入刚才新建项目的目录 sails lift //运行项目&#xff0c;运行原理也是直接在项目目录路径下使用node app.js npm …

Sails.js自动化Api实践与测试

开发中为了快速交互数据库&#xff0c;于是需要一个能便捷搭建api的平台。于是学习了一下sails.js框架。本次实践是一次摸索&#xff0c;使用了winston日志记录&#xff0c;supertest单元测试&#xff0c;mongo数据库&#xff0c;hashids哈希值解密。 模块: winstonsupertestmo…

五、解读Sails之Waterline源代码

文章目录 sql调试代码跟踪package.json启动调试Auto-Migrating备份原始数据删除所有表再重建回写备份数据 加密库 encrypted-attraes-256-gcm算法encrypted-att 的使用密钥 sql转义 sqlstring日期处理三种方式比较mariaDB&#xff08;或my-sql&#xff09;中的日期时间string 对…

一、Sails基础操作

本篇目录 Sails 安装App结构修改端口跨域问题第一个Api控制器用Postman 做Api调试MySql命令行操作MySql8.0版本加密问题 Sails 操作Mysql创建第一个model实现一个model的增删改查 Sails 安装 Sailsjs提供安装脚手架&#xff0c;使用之前可以先安装Sailjs npm install sails -…

sails

sails介绍 node.js的MVC框架&#xff0c;完全继承Express&socket.io的一些API 使用 全局安装 npm install -g sails创建项目 sails new 项目名称选2 选2 启动项目 sails liftsails框架目录介绍 api MVC结构项目代码目录controller层controller层尽量只做数据封装&…

Sails基础之Models层的config/datastores配置

配置与使用 Sails提供并支持多种Models层的存储&#xff08;https://sailsjs.com/documentation/concepts/extending-sails/adapters/available-adapters&#xff09;&#xff0c; 使用时需要在应用程序项目下安装对应的adapter并且在config/datastores或config/env/productio…

Sails的简单学习

这里贴出Sails的官方 一.Sails的简单介绍 官网上说&#xff1a; The web framework of your dreams.你梦想中的web框架。 Sails让创建自定义、企业级的Node.js应用的工作变得简单。它模拟了大家熟悉的诸如Ruby on Rails这种框架的MVC设置模式&#xff0c;但是也拥有满足现代…

什么是Sails

Sails的关键字 Realtime MVC Framework for Node.js Node.js Sails采用纯粹的Node.js进行构建&#xff0c;你只需要掌握一门javascript编程语言就可以构建Sails应用程序&#xff08;Web程序&#xff09;&#xff1b; MVC Framework Sails提供了基于MVC结构组织Web程序的基础…

sublime插件anaconda的设置

在 python 编辑环境下&#xff0c;使用 anaconda 完成一些代码补全和提示 具体设置如下 {//由于Anaconda插件本身无法知道Python安装的路径&#xff0c;所以需要设置Python主程序的实际位置"python_interpreter": "../python.exe",//忽略各种空格不对, 超…

Sublime 插件安装

1、百度搜索 Sublime3&#xff0c;进入 Sublime3 官网下载安装文件 注意&#xff1a;不要走到 Sublime2 的官网去下载&#xff0c;也不要使用 Sublime 的其它中文汉化版本&#xff1b; 2、下载 github 的 Package Control 包&#xff0c;下载地址&#xff1a; https://githu…

Sublime插件安装

1.PackageControl 功能&#xff1a;安装包管理 简介&#xff1a;sublime插件控制台&#xff0c;提供添加、删除、禁用、查找插件等功能 使用&#xff1a;https://sublime.wbond.net/installation 安装方法&#xff1a; CTRL &#xff0c;出现控制台粘贴以下代码至控制台 …

解决sublime无法下载插件问题

解决sublime无法下载插件问题 最近遇到了无法在sublime下载插件的问题&#xff0c;解决方法如下。 首先下载一个文件&#xff0c;地址如下 https://pan.baidu.com/s/1OlC0q8MwiZ_cEbs56SIwzw&#xff0c;提取码为vef9 下载完成后将其解压 再放入sublime文件夹中 接着点开P…

mac sublime安装插件

sublime 安装插件 需要等待一会&#xff0c;在弹出的新窗口&#xff0c;输入要添加的插件的名称&#xff0c;选择确认就可以了 以 Pretty JSON 插件为例&#xff0c;在将json字符串粘贴到文件之后&#xff0c;使用&#xff1a; control command j 进行格式化 调用搜索栏 shi…

java开发sublime插件_开发者最常用的8款Sublime text 3插件

5. SublimeCodeIntel Sublime​Code​Intel 作为一个代码提示和补全插件&#xff0c;支持Javascript、Mason、XBL、XUL、RHTML、SCSS、python、HTML、Ruby、Python3、XML、Sass、XSLT、Django、HTML5、Perl、CSS、Twig、Less、Smarty、Node.js、Tcl、TemplateToolkit 和 PHP 等…