Skip to content

docs: change README & comment & UI to English #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
3 commits merged into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 7 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,13 @@
# Node.js Getting started
在 LeanCloud 云引擎上使用 Express 的 Node.js 示例项目。

若希望获取更多常见功能和代码片段,请访问 [leancloud/leanengine-nodejs-demos](https://github.com/leancloud/leanengine-nodejs-demos)。
A simple Node.js application based on [express] for LeanEngine Node.js runtime.

## 本地运行
[express]: https://expressjs.com/

首先确认本机已经安装 [Node.js](http://nodejs.org/) 运行环境和 [LeanCloud 命令行工具](https://leancloud.cn/docs/leanengine_cli.html),然后执行下列指令:
## Documentation

```
git clone https://github.com/leancloud/node-js-getting-started.git
cd node-js-getting-started
```

安装依赖:

```
npm install
```

登录并关联应用:

```
lean login
lean switch
```

启动项目:

```
lean up
```

之后你就可以在 [localhost:3000](http://localhost:3000) 访问到你的应用了。

## 部署到 LeanEngine

部署到预备环境(若无预备环境则直接部署到生产环境):

```
lean deploy
```

## 相关文档

* [云函数开发指南](https://leancloud.cn/docs/leanengine_cloudfunction_guide-node.html)
* [网站托管开发指南](https://leancloud.cn/docs/leanengine_webhosting_guide-node.html)
* [JavaScript 开发指南](https://leancloud.cn/docs/leanstorage_guide-js.html)
* [Node.js Web Hosting Guide](https://docs.leancloud.app/leanengine_webhosting_guide-node.html)
* [Node.js Cloud Function Guide](https://docs.leancloud.app/leanengine_cloudfunction_guide-node.html)
* [LeanStorage JavaScript Guide](https://docs.leancloud.app/leanstorage_guide-js.html)
* [JavaScript SDK API](https://leancloud.github.io/javascript-sdk/docs/)
* [Node.js SDK API](https://github.com/leancloud/leanengine-node-sdk/blob/master/API.md)
* [命令行工具使用指南](https://leancloud.cn/docs/leanengine_cli.html)
* [云引擎常见问题和解答](https://leancloud.cn/docs/leanengine_faq.html)
* [lean-cli Guide](https://docs.leancloud.app/leanengine_cli.html)
23 changes: 12 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var AV = require('leanengine');

// 加载云函数定义,你可以将云函数拆分到多个文件方便管理,但需要在主文件中加载它们
// Loads cloud function definitions.
// You can split it into multiple files but do not forget to load them in the main file.
require('./cloud');

var app = express();

// 设置模板引擎
// Configures template engine.
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// 设置默认超时时间
// Configures default timeout.
app.use(timeout('15s'));

// 加载云引擎中间件
// Loads LeanEngine middleware.
app.use(AV.express());

app.enable('trust proxy');
// 需要重定向到 HTTPS 可去除下一行的注释。
// Uncomment the following line to redirect all HTTP requests to HTTPS.
// app.use(AV.Cloud.HttpsRedirect());

app.use(express.static('public'));
Expand All @@ -36,11 +37,11 @@ app.get('/', function(req, res) {
res.render('index', { currentTime: new Date() });
});

// 可以将一类的路由单独保存在一个文件中
// You can store routings in multiple files according to their categories.
app.use('/todos', require('./routes/todos'));

app.use(function(req, res, next) {
// 如果任何一个路由都没有返回响应,则抛出一个 404 异常给后续的异常处理器
// If there is no routing answering, throw a 404 exception to exception handlers.
if (!res.headersSent) {
var err = new Error('Not Found');
err.status = 404;
Expand All @@ -51,7 +52,7 @@ app.use(function(req, res, next) {
// error handlers
app.use(function(err, req, res, next) {
if (req.timedout && req.headers.upgrade === 'websocket') {
// 忽略 websocket 的超时
// Ignores websocket timeout.
return;
}

Expand All @@ -60,13 +61,13 @@ app.use(function(err, req, res, next) {
console.error(err.stack || err);
}
if (req.timedout) {
console.error('请求超时: url=%s, timeout=%d, 请确认方法执行耗时很长,或没有正确的 response 回调。', req.originalUrl, err.timeout);
console.error('Request timeout: url=%s, timeout=%d, please check whether its execution time is too long, or the response callback is invalid.', req.originalUrl, err.timeout);
}
res.status(statusCode);
// 默认不输出异常详情
// Do not output exception details by default.
var error = {};
if (app.get('env') === 'development') {
// 如果是开发环境,则将异常堆栈输出到页面,方便开发调试
// Displays exception stack on page if running in the development enviroment.
error = err;
}
res.render('error', {
Expand Down
4 changes: 2 additions & 2 deletions cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const fs = require('fs')
const path = require('path')

/**
* 加载 functions 目录下所有的云函数
* Loads all cloud functions under the `functions` directory.
*/
fs.readdirSync(path.join(__dirname, 'functions')).forEach( file => {
require(path.join(__dirname, 'functions', file))
})

/**
* 一个简单的云代码方法
* A simple cloud function.
*/
AV.Cloud.define('hello', function(request) {
return 'Hello world!'
Expand Down
7 changes: 3 additions & 4 deletions routes/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var AV = require('leanengine');

var Todo = AV.Object.extend('Todo');

// 查询 Todo 列表
// Todo list
router.get('/', function(req, res, next) {
var query = new AV.Query(Todo);
query.descending('createdAt');
Expand All @@ -15,8 +15,7 @@ router.get('/', function(req, res, next) {
});
}, function(err) {
if (err.code === 101) {
// 该错误的信息为:{ code: 101, message: 'Class or object doesn\'t exists.' },说明 Todo 数据表还未创建,所以返回空的 Todo 列表。
// 具体的错误代码详见:https://leancloud.cn/docs/error_code.html
// Todo class does not exist in the cloud yet.
res.render('todos', {
title: 'TODO 列表',
todos: []
Expand All @@ -27,7 +26,7 @@ router.get('/', function(req, res, next) {
}).catch(next);
});

// 新增 Todo 项目
// Creates a new todo item.
router.post('/', function(req, res, next) {
var content = req.body.content;
var todo = new Todo();
Expand Down
8 changes: 4 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ AV.init({
masterKey: process.env.LEANCLOUD_APP_MASTER_KEY
});

// 如果不希望使用 masterKey 权限,可以将下面一行删除
// Comment the following line if you do not want to use masterKey.
AV.Cloud.useMasterKey();

var app = require('./app');

// 端口一定要从环境变量 `LEANCLOUD_APP_PORT` 中获取。
// LeanEngine 运行时会分配端口并赋值到该变量。
// Retrieves the port number from environment variable `LEANCLOUD_APP_PORT`.
// LeanEngine runtime will assign a port and set the environment variable automatically.
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);

// 注册全局未捕获异常处理器
// Registers a global exception handler for uncaught exceptions.
process.on('uncaughtException', function(err) {
console.error('Caught exception:', err.stack);
});
Expand Down
6 changes: 3 additions & 3 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</head>
<body>
<h1>LeanEngine</h1>
<p>这是 LeanEngine 的示例应用</p>
<p>当前时间:<%= currentTime %></p>
<p><a href="/todos">一个简单的「TODO 列表」示例</a></p>
<p>This is a LeanEngine demo application.</p>
<p>Current date: <%= currentTime %></p>
<p><a href="/todos">A simple todo demo</a></p>
</body>
</html>
2 changes: 1 addition & 1 deletion views/todos.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<h1><%= title %></h1>
<form action="/todos" method="POST">
<input type="text" name="content" />
<input type="submit" value="新增" />
<input type="submit" value="New todo" />
</form>
<ul>
<% for(var i=0; i<todos.length; i++) {%>
Expand Down