Giter Site home page Giter Site logo

leancloud / leanengine-nodejs-demos Goto Github PK

View Code? Open in Web Editor NEW
111.0 34.0 49.0 212 KB

云引擎 Node.js 常用功能和示例汇总

Home Page: https://leancloud.cn/docs/leanengine_webhosting_guide-node.html

JavaScript 98.81% EJS 1.19%
leanengine leancloud

leanengine-nodejs-demos's Introduction

LeanEngine Node.js Demos

该项目是 LeanEngine Node.js 项目的常用功能和示例仓库。包括了推荐的最佳实践和常用的代码片段,每个文件中都有较为详细的注释,适合云引擎的开发者阅读、参考,也可以将代码片段复制到你的项目中使用。

若希望从一个更精简的项目骨架开始开发你的新项目,请使用 leancloud/node-js-getting-started

功能列表(云函数)

下面列出的功能均以云函数实现,位于 functions 目录中。每个文件的开头已列出所需的依赖和配置(环境变量),你可以在安装依赖后将文件直接复制到你的 functions 目录中,我们的示例项目会自动加载这个目录中的文件。

对于需要 LeanCache 的功能,还请阅读下面的 LeanCache 段落。

✅表示写了测试,✨表示实现了相对完整的功能,无需修改即可使用。

文件名 提供的云函数 介绍
amr-transcoding.js amrToMp3 使用 ffmpeg 将 amr 音频转码为 mp3。
associated-data.js createPostSamples
getPostsWithAuthor
getPostWithAuthor
afterUpdate:_User
缓存关联数据示例(需要 LeanCache)。
batch-update.js batchUpdateByQuery
batchUpdateAll
批量更新数据示例。
captcha-cache.js getCaptchaImageCache
requestMobilePhoneVerifyCache
使用图形验证码限制短信接口(使用 LeanCache)(需要 LeanCache)。
captcha-storage.js getCaptchaImageStorage
requestMobilePhoneVerifyStorage
✅ 使用图形验证码限制短信接口(使用云存储后端)。
crawler.js crawlWebsite
crawling
爬虫示例,使用云队列抓取一个站点下的所有网页。
imagemagick.js imageMagicResize ✅ 使用 imageMagick 处理图像。
leaderboard.js submitHighest
getRankRange
getScoreRange
getRankAndScore
archiveLeaderboard
✨ 一个功能相对完整的排行榜,支持任意数量的用户排序、支持查询任意用户的排名、支持查询任意排名段的用户(需要 LeanCache)。
limited-stock-rush.js createRushStock
getOpeningRushs
rush
commitRushStock
✨ 使用 LeanCache 应对秒杀抢购活动中短时间的大量请求(需要 LeanCache)。
login-by-app.js requestLoginByApp
verifyByApp
loginByApp
通过移动端应用验证用户身份,登录网站(比如扫码登录)。
meta.js getEnvironments
getUser
getParams
getClientMeta
getHeaders
afterSave:HookObject
从运行环境或客户端读取元信息(环境变量、请求头等)。
pubsub.js publishMessage 使用 Redis Pub/Sub 收发消息。
queue-delay-retry.js queueDelayTask
queueRetryTask
delayTaskFunc
retryTaskFunc
云队列:延时和重试。云函数任务队列提供了一种可靠地对云函数进行延时运行、重试、结果查询的能力。
queue-result-query.js createTask
queryResult
longRunningTask
云队列:结果查询
readonly.js updateCategory
getCategories
afterUpdate:Category
afterSave:Category
afterDelete:Category
refreshCategories
热点只读数据缓存示例(需要 LeanCache)。
redlock.js startTaskLoop
getCurrentTask
用 LeanCache 实现分布式锁(需要 LeanCache)。
rtm-signature.js signLogin
signStartConversation
signOperateConversation
signQueryMessage
signBlockConversation
signBlockClient
✅ 使用云引擎实现即时通讯服务的签名。
todos.js getAllTodos
createTodo
deleteTodo
setTodoToDone
在云引擎中以客户端的权限来操作云存储。
weapp-decrypt.js decryptWeappData ✨ ✅ 解密微信小程序用户加密数据。
xml.js xmlBuildObject 使用云函数序列化 XML 对象。
rtm-onoff-status.js _clientOnline
_clientOffline
getOnOffStatus
即时通讯上下线状态的存储以及查询(需要 LeanCache)。

功能列表(网站托管)

下面列出的功能位于 routes 目录。每个文件的开头已列出所需的依赖和配置(环境变量),有些功能需要额外的配置,请阅读文件开头的说明,你可以在安装依赖后将文件直接复制到你的 routes 目录中,然后在 app.js 中引用,例如:

app.use('/wechat', require('./routes/wechat-message-callback'))
文件名 介绍
wechat-message-callback.js 接收并自动回复 微信公众平台的用户消息回调
websocket.js 简单的 WebSocket 示例:将客户端发来的消息原样发回客户端(echo)、每隔一秒向客户端发送一条消息(timer)
cookie-session.js 使用 Cookie Session 在 Cookie 中维持用户登录状态
render-ejs.js 使用 EJS 渲染 HTML 页面
markdown.js 项目主页,将 README.md 渲染成 HTML 显示在页面上

其他 Demo

还有一些功能相对完备的 Demo 被制作成了独立的应用:

Demo 地址 代码地址 介绍
snapcat.leanapp.cn snapcat 分支 一个使用 chrome-headless 的截图服务。
graphql.leanapp.cn leancloud/leancloud-graphql 运行在云引擎上的第三方 GraphQL 支持,允许你用 GraphQL 查询 LeanCloud 云存储中的所有数据。
leanticket.cn leancloud/ticket 运行在云引擎上的工单系统(即 LeanCloud 官方客服系统)
status.leancloud.cn leancloud/leancloud-status LeanCloud 服务状态页。

脚本

这些工具脚本位于 bin 目录:

文件名 使用方法 介绍
load-test load-test 30 对自定义的代码片段进行压力测试的工具,会给出速率和耗时等统计数据。

LeanCache

对于用到了 LeanCache 的功能,你需要在控制台上创建 LeanCache 实例,复制该项目根目录的 redis.js 到你的项目,并修改其中的 LeanCache 名称。

本地运行 Redis:

  • Mac 运行 brew install redis 安装,然后用 redis-server 启动。
  • Debian/Ubuntu 运行 apt-get install redis-server, CentOS/RHEL 运行 yum install redis.
  • Windows 尚无官方支持。

相关文档

leanengine-nodejs-demos's People

Contributors

aisk avatar binaryhb0916 avatar jysperm avatar nicecui avatar sdjcw avatar weakish avatar zapcannon87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leanengine-nodejs-demos's Issues

jedis demo

希望多增加一些java方面leancache的使用说明,目前来看java使用leancache,按照网上的教程来操作基本没法使用

ticket 37693

网上常见的 jedis 中文教程往往是针对大项目。针对简单项目的中文教程很少,开发者如果不熟悉 jedis,随便到网上去搜教程照抄就容易踩坑。所以提供一个简单的 demo 还是很有参考价值的。

AV.Cloud.HttpsRedirect没有重定向

var app = express();

// 设置 view 引擎
app.set('views', path.join(__dirname, '../public'));
app.engine('.html', require('ejs').__express);
app.set('view engine', 'html');

// 设置静态资源
app.use(express.static(path.join(__dirname, '../public')));

// 设置默认超时时间
app.use(timeout(15000, {}));

// 加载云函数定义
require('./cloud');
// 加载云引擎中间件
app.use(AV.express());

// 加载 cookieSession 以支持 AV.User 的会话状态
app.use(AV.Cloud.CookieSession({
secret: 'xxxxxxxxxxxxxxx',
maxAge: 1000 * 60 * 60 * 3,
fetchUser: false
}));

// 强制使用 https
app.enable('trust proxy');
app.use(AV.Cloud.HttpsRedirect());

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

按以上加载中间件的顺序,项目部署到云引擎后输入http://xx.leanapp.cn/没有自动重定向到https://xx.leanapp.cn

logout 不能清除登录用户信息

用lean up本地运行时,logout 不能清除登录用户信息。res.clearCurrentUser();不知道是没有起作用还是怎么回事。nodejs:6.9.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.