Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('response have captchaId and imageUrl', async () => {
const result = await AV.Cloud.run('getCaptchaImageStorage')
result.should.have.properties(['captchaId', 'imageUrl'])
captchaId = result.captchaId
await new AV.Query('Captcha').find().then( captchas => {
// 因为设置了 ACL,所以非特殊账号无法查询到 captcha 对象
captchas.length.should.equal(0)
})
})
})
'use strict';
var AV = require('leanengine');
AV.init({
appId: process.env.LEANCLOUD_APP_ID,
appKey: process.env.LEANCLOUD_APP_KEY,
masterKey: process.env.LEANCLOUD_APP_MASTER_KEY
});
AV.Cloud.useMasterKey();
var app = require('./app');
// 端口一定要从环境变量 `LEANCLOUD_APP_PORT` 中获取。
// LeanEngine 运行时会分配端口并赋值到该变量。
var PORT = parseInt(process.env.LEANCLOUD_APP_PORT || process.env.PORT || 3000);
app.listen(PORT, function (err) {
console.log('Node app is running on port:', PORT);
// 注册全局未捕获异常处理器
process.on('uncaughtException', function(err) {
console.error("Caught exception:", err.stack);
});
process.on('unhandledRejection', function(reason, p) {
console.error("Unhandled Rejection at: Promise ", p, " reason: ", reason.stack);
// **注意!** 环境变量中可能包含有有你自行添加的敏感信息(如第三方平台的密钥),因此该函数只会在开发环境下工作,请谨慎在线上应用中添加该函数。
AV.Cloud.define('getEnvironments', async request => {
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
// 去除 masterKey 和 LeanCache 连接字符串等含有敏感信息的环境变量
return _.mapValues(process.env, function(value, key) {
if (_.startsWith(key, 'REDIS_URL') || _.includes(['LC_APP_MASTER_KEY'], key)){
return null
} else {
return value
}
})
}
})
// 返回客户端的当前用户
AV.Cloud.define('getUser', async request => {
return request.currentUser
})
// 返回客户端的请求参数
AV.Cloud.define('getParams', async request => {
return request.params
})
// 返回客户端的额外元信息(IP 地址等)
AV.Cloud.define('getClientMeta', async request => {
return request.meta
})
// 返回客户端的请求头
AV.Cloud.define('getHeaders', async request => {
// 内部接口,请勿在业务代码中使用
* 热点只读数据缓存示例
*
* 在系统中有些数据是需要非常频繁地读取的,但这些数据量很小而且不常修改,比较适合整个放到 LeanCache 中
*
* 在这个示例中我们以缓存一个电商网站的商品分类信息为例。
*
* 安装依赖:
*
* npm install lodash
*/
const {redisClient} = require('../redis')
const Category = AV.Object.extend('Category')
/* 设置特定分类的信息,如不存在会新建,会触发 afterSave 或 afterUpdate 的 Hook */
AV.Cloud.define('updateCategory', async request => {
try {
const category = await new AV.Query(Category).equalTo('name', request.params.name).first()
if (category) {
return category.save(request.params)
} else {
return new Category().save(request.body)
}
} catch (err) {
if (err.code == 101) { // Class or object doesn't exists.
return new Category().save(request.body)
} else {
throw err
}
}
})
/* 从 Redis 中获取分类信息,不会查询云存储 */
AV.Cloud.define('getCategories', async request => {
const categories = await redisClient.hgetall('categories')
return categories.map( category => {
return AV.parseJSON(JSON.parse(category))
})
})
/* Redis 中的数据是通过下面三个 Class Hook 来和云存储保持同步的 */
AV.Cloud.afterUpdate('Category', async request => {
redisClient.hset('categories', request.object.get('name'), JSON.stringify(request.object.toFullJSON()))
})
AV.Cloud.afterSave('Category', async request => {
redisClient.hset('categories', request.object.get('name'), JSON.stringify(request.object.toFullJSON()))
})
AV.Cloud.afterDelete('Category', async request => {
redisClient.hdel('categories', request.object.get('name'))
})
/* 我们还可以设置一个一天的定时器,每天与云存储进行一次全量的同步,以免错过某个 Class Hook */
AV.Cloud.define('refreshCategories', async request => {
const categories = await new AV.Query(Category).find()
const categorieSerialized = _.mapValues(_.keyBy(categories, category => category.get('name')), object => JSON.stringify(object.toFullJSON()))
await redisClient.hmset('categories', categorieSerialized)
})
const status = request.params.status || 'a'
const createQuery = () => {
return new AV.Query(Post).notEqualTo('status', status)
}
await batchUpdateByQuery(createQuery, (object) => {
console.log('performUpdate for', object.id)
object.set('status', status)
return object.save()
})
console.log('batch update finished')
})
AV.Cloud.define('batchUpdateAll', async request => {
const status = request.params.status || 'a'
const createQuery = () => {
return new AV.Query(Post)
}
await batchUpdateAll(createQuery, (object) => {
console.log('performUpdate for', object.id)
object.set('status', status)
return object.save()
})
console.log('batch update finished')
})
/*
const uuid = require('uuid/v4');
const AV = require('leanengine');
const Order = require('./order');
const wxpay = require('./wxpay');
/**
* 一个简单的云代码方法
*/
AV.Cloud.define('hello', function(request, response) {
response.success('Hello world!');
});
/**
* 小程序创建订单
*/
AV.Cloud.define('order', (request, response) => {
const user = request.currentUser;
if (!user) {
return response.error(new Error('用户未登录'));
}
const authData = user.get('authData');
if (!authData || !authData.lc_weapp) {
return response.error(new Error('当前用户不是小程序用户'));
}
const order = new Order();
* Created by xiadd on 6/30/16.
*/
var AV = require('leanengine');
/**
* 一个简单的云代码方法
*/
AV.Cloud.define('hello', function (request, response) {
response.success('Hello world!');
});
AV.Cloud.define('getUsers', function (req, res) {
});
module.exports = AV.Cloud;
* 使用 imageMagick 处理图像
*
* 安装依赖:
*
* npm install gm
*
* 在 `leanengine.yaml` 中添加:
*
* systemDependencies:
* - imagemagick
*
*/
const imageMagick = gm.subClass({imageMagick: true})
AV.Cloud.define('imageMagicResize', async request => {
return new Promise( (resolve, reject) => {
imageMagick('public/leanstorage.png').resize(91, 77).toBuffer('png', (err, buffer) => {
if (err) {
reject(err)
} else {
resolve({
imageUrl: 'data:image/png;base64,' + buffer.toString('base64')
})
}
})
})
})