,
167 | document.getElementById('root')
168 | )
169 | ```
170 |
171 | 此时,一个基于 Webpack 手动搭建的简易型“Hello World”App 开发完成,可以通过如下命令本地运行。
172 |
173 | ```bash
174 | $ webpack-dev-server --mode development --open --hot
175 | ```
176 |
177 | 更多自定义内容如添加 devServer 支持、添加多页应用支持...等各种各样新技术栈的支持,也可以引申实战。
178 |
179 | ## 因此,我们需要 CRA
180 |
181 | 可见,不使用构建工具编写不切实际,使用构建工具手动搭建 React App 又很繁琐。因此我们需要一个能初始化一个可直接运行项目的工具,并提供各种简易的插件,Create React App 应运而生。
182 |
183 | > CRA 适用于中小型 React 项目。
184 |
185 | ### CRA 的设计哲学
186 |
187 | - 一种依赖关系。尽管 CRA 使用了 Webpack,Babel,ESLint 等各种出色的项目,但我们只需要 CRA 一种依赖;从 CRA 生成项目的 package.json 中也可以看到并没有 Webpack、Babel 的痕迹。
188 | - 无需配置。我们无需进行任何额外配置便可以直接运行代码,专注业务开发;同时 CRA 还提供了开发和生产版本的合理配置。
189 | - 非锁定配置。只需要运行一个命令, 所有的配置项和构建依赖项都将直接“弹出”到项目中,交由我们来修改。
190 |
191 | ### CRA 包含了什么?
192 |
193 | CRA 将具有构建现代单页 React 应用所需的一切:
194 |
195 | - React,JSX,ES6,TypeScript 和 Flow 语法支持;
196 | - ES6+ 标准的支持,例如对象传播运算符;
197 | - CSS 自动添加前缀的支持,因此我们不需要 -webkit- 或其他前缀;
198 | - 快速的交互式单元测试运行程序,内置对覆盖率报告的支持;
199 | - 实时开发服务器,警告常见错误;
200 | - 一个构建脚本,用于将 JS,CSS 和图像与哈希和源映射捆绑在一起进行生产;
201 | - 满足所有渐进式 Web 应用程序标准的 [ServiceWorker](https://developers.google.com/web/fundamentals/getting-started/primers/service-workers) 和 [Web 应用程序清单](https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/) 支持 (注意:从react-scripts@2.0.0及更高版本开始支持 ServiceWorker);
202 | - 单一依赖项即可轻松更新上述工具。
203 |
204 | ### CRA 的使用场景
205 |
206 | Create React App 非常适合:
207 |
208 | - 在方便且功能丰富的开发环境中学习 React;
209 | - 快速启动新的单页 React 应用程序;
210 | - 快速使用 React 为库和组件创建示例。
211 |
212 | 如果我们想在没有数百个传递构建工具依赖的情况下尝试 React,请考虑使用单个 HTML 文件构建或使用在线沙箱平台构建。
213 |
214 | - 如果需要将 React 代码与服务器端模板框架(如 Rails,Django 或 Symfony)集成,或者如果不构建单页应用,请考虑使用更灵活的 [nwb](https://github.com/insin/nwb) 或 [Neutrino](https://neutrino.js.org/)。特别是对于 Rails,可以使用 [Rails Webpacker](https://github.com/rails/webpacker)。对于 Symfony,请尝试 [Symfony's Webpack Encore](https://symfony.com/doc/current/frontend/encore/reactjs.html);
215 | - 如果需要发布 React 组件,[nwb](https://github.com/insin/nwb) 以及 Neutrino 的 [react-components preset](https://neutrino.js.org/packages/react-components/) 也可以这样做;
216 | - 如果要使用 React 和 Node.js 进行服务器渲染,请查看 [Next.js](https://github.com/zeit/next.js/) 或 [Razzle](https://github.com/jaredpalmer/razzle)。 Create React App 与后端无关,仅生成静态HTML / JS / CSS包;
217 | - 如果网站大部分是静态的(例如,投资组合或博客),请考虑改用 [Gatsby](https://www.gatsbyjs.org/)。与 Create React App 不同,它在构建时会将网站预先渲染为 HTML;
218 | - 最后,如果需要更多自定义设置,请查看 Neutrino 及其 [React preset](https://neutrino.js.org/packages/react/)。
219 |
220 | ### CRA 支持的浏览器
221 |
222 | 一些支持的浏览器规则如下:
223 |
224 | - 默认情况下,生成的项目支持所有现代浏览器。 对 Internet Explorer 9、10 和 11 的支持需要 polyfill。 对于一组支持旧版浏览器的 polyfill,请使用 [react-app-polyfill](https://github.com/facebook/create-react-app/blob/master/packages/react-app-polyfill/README.md);
225 | - 默认情况下,生成的项目在 package.json 文件中包含一个 browserslist 配置,以针对基于全球使用情况(> 0.2%)的广泛浏览器(用于生产构建)和用于开发的现代浏览器。 这提供了良好的开发体验,尤其是在使用异步/等待等语言功能时,但仍与生产中的许多浏览器保持高度兼容性;
226 | - browserslist 配置控制输出的 JavaScript,以使注入的代码与指定的浏览器兼容。 通过运行构建脚本来创建生产构建时,将使用生产列表,而在运行启动脚本时,将使用开发列表。 可以使用 [https://browserl.ist](https://browserl.ist) 查看配置的浏览器列表支持的浏览器;
227 | - 请注意,这不会自动包括 polyfills,仍然需要根据所支持的浏览器来添加语言功能(见上文);
228 | - 在编辑 browserslist 配置时,我们的更改可能不会立即被获取。 这是由于 babel-loader 中的一个未检测到 package.json 中更改的问题。 一种快速的解决方案是删除 node_modules/.cache 文件夹,然后重试。
229 |
230 | 这里的重点是 BrowsersList,一个“在不同的前端工具之间共用目标浏览器和 node 版本的配置工具”。简而言之,就是 Babel 等转移工具通过我们设置的 BrowsersList 中想要支持的浏览器版本来决定哪些语法需要被编译。
231 |
232 | ### CRA 支持的 ES 标准
233 |
234 | 一些支持的 ES 标准规则如下:
235 |
236 | - 该项目支持最新 JavaScript 标准的超集。 除ES6语法功能外,它还支持:
237 | - [Exponentiation Operator](https://github.com/rwaldron/exponentiation-operator) (ES2016);
238 | - [Async/await](https://github.com/tc39/ecmascript-asyncawait) (ES2017);
239 | - [Object Rest/Spread Properties](https://github.com/tc39/proposal-object-rest-spread) (ES2018);
240 | - [Dynamic import()](https://github.com/tc39/proposal-dynamic-import) (stage 4 proposal);
241 | - [Class Fields and Static Properties](https://github.com/tc39/proposal-class-public-fields) (part of stage 3 proposal);
242 | - [JSX](https://facebook.github.io/react/docs/introducing-jsx.html), [Flow](https://create-react-app.dev/docs/adding-flow) and [TypeScript](https://create-react-app.dev/docs/adding-typescript);
243 | - Learn more about [different proposal stages](https://tc39.github.io/process-document/)。
244 | - 尽管 Facebook 建议谨慎使用实验性功能,但 Facebook 会在产品代码中大量使用这些功能,因此,如果将来任何这些建议发生更改,Facebook 都打算提供 [codemod](https://medium.com/@cpojer/effective-javascript-codemods-5a6686bb46fb);
245 | - 请注意,默认情况下,该项目不包含任何 [polyfill](https://github.com/facebook/create-react-app/blob/master/packages/react-app-polyfill/README.md);
246 | - 如果您使用任何其他需要运行时支持的ES6 +功能(例如Array.from()或Symbol),请确保[手动包括适当的polyfill](https://github.com/facebook/create-react-app/blob/master/packages/react-app-polyfill/README.md),或者您要定位的浏览器已支持它们。
247 |
248 | ### CRA 的两个核心库
249 |
250 | Create React App 有两个核心库,如下:
251 |
252 | - create-react-app 是全局命令,用于创建初始化的 React 项目;
253 | - react-scripts 是所生成的项目中的开发依赖项,包括运行项目、测试项目、打包项目等多种命令。由于 CRA 的一种依赖性原则,react-scripts 便开放了所有内部其它依赖的使用方式。
254 |
255 | ## 实战 3:使用 CRA 构建 React App
256 |
257 | 到这里,我们终于需要通过命令行来安装和使用 CRA,来构建我们的第三个“Hello World”App。
258 |
259 | ### 全局安装 CRA
260 |
261 | 为保证每一个新项目都能使用到 CRA 最新最全的功能,请确保 CRA 为最新版本。
262 |
263 | ```bash
264 | # 在已安装 CRA 的情况下,可以先卸载 CRA
265 | $ npm uninstall -g create-react-app
266 | # 正式安装 CRA
267 | $ npm install -g create-react-app
268 | ```
269 |
270 | ### 初始化 CRA 项目
271 |
272 | 根据我们的 npm 版本,选择相应命令来安装最新版的 CRA 并初始化第一个项目。同时检查自己的 node 版本,需要在本地开发计算机上安装 Node 8.16.0 或 Node 10.16.0 或更高版本(但服务器上不需要)。 我们可以使用nvm(macOS / Linux)或 nvm-windows 在不同项目之间切换Node版本。
273 |
274 | ```bash
275 | # 查看自己的 npm 版本
276 | $ npm --version
277 |
278 | # 第一种新建项目方式——npm 5.2+ 时,以下命令会安装最新版 CRA
279 | $ npx create-react-app my-app
280 | # 第一种新建项目方式——npm 版本小于等于 5.1 时
281 | $ create-react-app my-app
282 |
283 | # 第二种新建项目方式
284 | # npm 6+ 开始支持 npm init
285 | $ npm init react-app my-app
286 |
287 | # 第三种新建项目方式
288 | $ yarn create react-app my-app
289 | ```
290 |
291 | 
292 |
293 | ### 项目的文件结构
294 |
295 | 通过命令行的构建,我们初始化了第一个 CRA 项目,其中帮我们生成的项目目录结构如下(只有 src 下的文件才会被 Webpack 处理,只有 public 下的文件才能被 public/index.html 使用):
296 |
297 | ```bash
298 | my-app
299 | ├── .git # 隐藏文件夹,会初始化第一个 Commit 记录
300 | ├── README.md
301 | ├── node_modules
302 | ├── package.json # 依赖配置文件
303 | ├── .gitignore
304 | ├── [floder_name] # 根目录下可以建立其他文件夹,但不会被用在生产环境中
305 | ├── public # 只有 public 下的文件才能被 public/index.html 使用
306 | │ ├── favicon.ico
307 | │ ├── index.html # public/index.html 页面模板
308 | │ └── manifest.json
309 | └── src # 只有 src 下的文件才会被 Webpack 处理
310 | ├── App.css
311 | ├── App.js
312 | ├── App.test.js
313 | ├── [floder_name] # 可以建立其他文件夹,以被 Webpack 成功导入
314 | ├── index.css
315 | ├── index.js # JavaScript 打包入口文件
316 | ├── logo.svg
317 | └── serviceWorker.js
318 | ```
319 |
320 | 关于 package.json、index.js 和 public/index.html 文件夹,我们通过“实战 2”已经有所了解。前者是 JavaScript 打包入口文件,通常链接整个业务代码;后者是页面模板,是打包后整个静态页面的总入口。
321 |
322 | 这里对以下两个文件的出现进行简要的意义概括。
323 | _
324 |
325 | - src/serviceWorker.js:提供渐进式 Web 应用的核心功能,不论网络状况如何都能立即加载,并且在不需要网络请求(离线时)的情况下也能展示 UI ;
326 | - public/manifest.json:是渐进式 Web 应用将自身添加至桌面的功能依赖文件,也可以对图标、名称等信息进行配置。
327 |
328 | ### 运行 CRA 项目
329 |
330 | CRA 默认提供了运行、测试、打包、部署以及弹出项目的命令。其中的一些贴士:
331 |
332 | - npm start 内置热更新机制,代码改动时页面自动刷新;
333 | - npm test 以交互方式运行测试观察程序,默认情况下运行与自上次提交以来更改的文件相关的测试;
334 | - npm run build 将要生产的应用程序生成到生成文件夹。它在生产模式下正确捆绑了React,并优化了构建以获得最佳性能。生成文件被压缩,并且文件名包含哈希;
335 | - npm run eject 将内置的各种 Webpack 配置弹出到项目中,让我们可以进行自定义。同时此操作不可逆,意味着我们承担了弹出配置后的风险。通常不推荐弹出,可以通过 React App Rewired 库进行配置注入。
336 |
337 | ```bash
338 | # ---- 运行 ----
339 | $ npm start
340 | $ open http://localhost:3000
341 | # ---- 测试 ----
342 | $ npm test
343 | # ---- 打包 ----
344 | $ npm run builds
345 | # ---- 弹出配置 ----
346 | $ npm run eject
347 | ```
348 |
349 | ### 搭建 CRA 生态
350 |
351 | 根据官方文档的思路,我们还能从更多角度拓展 CRA 的使用边界,下面进行概要介绍。
352 |
353 | - 为开发环境添加额外功能:包括“配置编辑器风格”、“开发隔离组件”、“分析打包文件”和“添加 HTTPS 支持”;
354 | - 添加样式与静态资源支持:包括“添加样式表文件”、“添加 CSS Modules 支持”、“添加 Sass 支持”、“添加 PostCSS 支持”、“添加图片文字和字体支持”、“添加 GraphQL 支持”、“使用 public 文件夹”、“进行代码拆分”;
355 | - 添加业务驱动支持:包括安装各种依赖项如“BootStrap”、“Flow”、“TypeScript”、“Delay”、“Router”,以及“导出组件”、“使用全局变量”、“配置环境变量”、“制作渐进式 Web 应用”和“创建生产环境”;
356 | - 添加测试支持:包括“运行测试”和“调试测试”;
357 | - 添加后端集成支持:包括“在开发环境中代理 API 请求”、“使用 AJAX 请求获取数据”、“集成后端 API”和“使用 Title & Meta 标签”;
358 | - 部署进阶:包括“静态服务器”、“Azure”、“Firebase”、“Github Pages”等平台的部署等。
359 |
360 | 这里无法深入展开,每一个点都可以是一个新的实战,当我们需要某个功能时便可以查阅相关文档来主动探索。其中“分析打包文件”的解读见“实战 4”。
361 |
362 | ## 实战 4:使用 Source Map Explorer 分析打包文件
363 |
364 | ```bash
365 | # 安装文件分析工具 source-map-explorer
366 | $ sudo npm install --save source-map-explorer
367 | # 打包项目
368 | $ npm run build
369 | # 将如下命令放入 package.json 中并生成快捷方式 npm run analyze
370 | # $ source-map-explorer 'build/static/js/*.js'
371 | # 注意此命令直接在命令行输入会提示找不到相关命令
372 | $ npm run analyze
373 | ```
374 |
375 | 对于一个刚被 CRA 生成的 React App 来说,分析的结果如下,包大小总计 129.38k。
376 |
377 | 
378 |
379 | ## 实战 5:在已有的 React 项目中引入/升级 CRA
380 |
381 | 回到刚才“实战 2”建立的 react-webpack-steper 项目中,当我们已经编写了一部分业务时,能否直接在当前项目中无痛引入 CRA?
382 |
383 | 解决思路便是:在大多数情况下,更改 package.json 中的 react-scripts 版本并删除不必要依赖配置,接着在此文件夹中运行 npm install 就足够了,但最好参考更改日志以了解潜在的重大更改。CRA 致力于将重大更改保持在最低限度,以便可以轻松升级 React 脚本。
384 |
385 | ```bash
386 | # 卸载 CRA 本身已经提供的依赖
387 | $ sudo npm uninstall --save webpack webpack-cli webpack-dev-server
388 | $ sudo npm uninstall --save-dev @babel/cli @babel-core @babel/preset-env @babel/preset-react
389 | $ sudo npm uninstall --save-dev babel-loader babel-plugin-module-resolver html-webpack-plugin
390 | # 删除 CRA 不需要使用的文件
391 | $ rm webpack.config.js .babelrc
392 | # 删除 node_modules
393 | $ rm -rf node_modules
394 | # 手动安装 React Script
395 | $ sudo npm install --save react-scripts@latest
396 | # 由于 CRA 默认规则,将 src/index.html 移至 public/index.html
397 | $ mkdir public
398 | $ mv src/index.html public
399 | # 在 package.json 中添加 React Script 启动命令
400 | $ vim package.json
401 | ```
402 |
403 | package.json 中添加/覆盖如下指令。
404 |
405 | ```bash
406 | "scripts": {
407 | "start": "react-scripts start",
408 | "build": "react-scripts build",
409 | "test": "react-scripts test",
410 | "eject": "react-scripts eject",
411 | ```
412 |
413 | 再次执行即可。由于每个人的具体配置不一定一致,可根据自身所遇问题进行搜索。升级原理类似。
414 |
415 | ```bash
416 | # 当没有 BrowsersList 时,CRA 会进行询问并帮助我们生成
417 | $ npm start
418 | ```
419 |
420 | ## 实战 6:使用 React App Rewired 注入新配置
421 |
422 | CRA 官方并不推荐使用 npm run eject 弹出配置,这会增加更多的 Webpack 维护工作。对于实在想改的 Webpack 配置来说,我们可以使用 [React App Rewired](https://github.com/timarney/react-app-rewired) 库进行配置注入,这里来做个小例子。
423 |
424 | > 此工具可以在不 'eject' 也不创建额外 react-scripts 的情况下修改 create-react-app 内置的 webpack 配置,然后你将拥有 create-react-app 的一切特性,且可以根据你的需要去配置 webpack 的 plugins, loaders 等。
425 |
426 | 继续使用 react-webpack-steper 项目,我们的简易目标是增加 devServer 本地代理。
427 |
428 | **第一步:安装依赖并进行基础配置**
429 |
430 | ```bash
431 | # 安装依赖
432 | $ sudo npm install --save-dev react-app-rewired customize-cra
433 | # 根目录建立 config-overrides.js
434 | $ touch config-overrides.js
435 | # 修改 package.json
436 | $ vim package.json
437 | # 运行项目
438 | $ npm start
439 | ```
440 |
441 | 其中,config-overrides.js 的初始代码为:
442 |
443 | ```javascript
444 | /* config-overrides.js */
445 | module.exports = function override(config, env) {
446 | //do stuff with the webpack config...
447 | return config;
448 | }
449 | ```
450 |
451 | package.json 的修改思路为:
452 |
453 | ```javascript
454 | /* package.json */
455 | "scripts": {
456 | - "start": "react-scripts start",
457 | + "start": "react-app-rewired start",
458 | - "build": "react-scripts build",
459 | + "build": "react-app-rewired build",
460 | - "test": "react-scripts test --env=jsdom",
461 | + "test": "react-app-rewired test --env=jsdom",
462 | "eject": "react-scripts eject"
463 | }
464 | ```
465 |
466 | **第二步:编写配置,进行代理**
467 |
468 | ```bash
469 | # 新增配置文件
470 | $ mkdir config
471 | $ touch config/proxy.js
472 | # 修改 config-overrides.js
473 | $ vim config-overrides.js
474 | ```
475 |
476 | 其中,config/proxy.js 源码是:
477 |
478 | ```javascript
479 | module.exports = {
480 | '/api/**': {
481 | target: 'http://110.114.120.120:8080',
482 | secure: false,
483 | changeOrigin: false,
484 | },
485 | }
486 | ```
487 |
488 | config-overrides.js 修改为:
489 |
490 | ```javascript
491 | const { overrideDevServer } = require('customize-cra')
492 | const proxy = require('./config/proxy')
493 |
494 | module.exports = {
495 | devServer: overrideDevServer((config) => {
496 | config.proxy = proxy
497 | return config
498 | }),
499 | }
500 | ```
501 |
502 | 此时,本地的所有 api 开头的接口请求都会被转发到 [http://110.114.120.120:8080](http://110.114.120.120:8080) 的模拟后端 IP 上。
503 |
504 | ## 对 CRA 未来版本的简单展望
505 |
506 | 截止目前(2020-01-10),CRA 的最新版本是 v3.3.0,我们可以从 Github 的 MileStone 中看到未来可能会改善的功能,其中整理并如下所述。
507 |
508 | - v3.x:添加多入口文件支持(不只是一个 index.js 入口);使用 worker-loader 添加对 WebWorker 的支持;更早地检查 Node 的版本;添加对子资源完整性 SRI 支持;生产环境中预加载脚本和链接...
509 | - v4.0:支持 Webpack 5.0(Webpack 目前最新版 v4.41.5,v5 也推出了一年多内测版);在 tsconfig.json 和 jsconfig.json 里新增对 baseUrl 和 paths 的支持(方便写 @ 绝对路径等);支持 Jest 配置中设置browser 为 true(根据环境提供正确的 Node 或 Browser 模块)...
510 | - v100.0:提供构建过程中的监视模式;适用于 Hooks 的热加载...
511 |
512 | 让我们一起持续关注。
513 |
514 | ## 结语
515 |
516 | 回顾文章,我们从初始化 React App 的多种方式,引出 CRA 的必要性再对其进行较为充分的解释,最后配上 6 个角度来从一些角度对 CRA 的使用方式进行了实战,最后回归到 CRA 的版本展望之中。
517 |
518 | 感谢你的阅读,如果你有什么更多的疑惑,CRA 的官方文档 + 开源仓库一定会满足你的一切。
519 |
520 | 
521 |
522 | 最后,一起拜读一下 CRA 和 Redux 作者、React 的核心贡献者 Dan Abramov 发布的这篇“[我的十年回顾](https://overreacted.io/my-decade-in-review/)”文章。
523 |
524 | 现在我们可以开始正式深入地学习 React 技术栈了。
525 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Attribution 4.0 International
2 |
3 | =======================================================================
4 |
5 | Creative Commons Corporation ("Creative Commons") is not a law firm and
6 | does not provide legal services or legal advice. Distribution of
7 | Creative Commons public licenses does not create a lawyer-client or
8 | other relationship. Creative Commons makes its licenses and related
9 | information available on an "as-is" basis. Creative Commons gives no
10 | warranties regarding its licenses, any material licensed under their
11 | terms and conditions, or any related information. Creative Commons
12 | disclaims all liability for damages resulting from their use to the
13 | fullest extent possible.
14 |
15 | Using Creative Commons Public Licenses
16 |
17 | Creative Commons public licenses provide a standard set of terms and
18 | conditions that creators and other rights holders may use to share
19 | original works of authorship and other material subject to copyright
20 | and certain other rights specified in the public license below. The
21 | following considerations are for informational purposes only, are not
22 | exhaustive, and do not form part of our licenses.
23 |
24 | Considerations for licensors: Our public licenses are
25 | intended for use by those authorized to give the public
26 | permission to use material in ways otherwise restricted by
27 | copyright and certain other rights. Our licenses are
28 | irrevocable. Licensors should read and understand the terms
29 | and conditions of the license they choose before applying it.
30 | Licensors should also secure all rights necessary before
31 | applying our licenses so that the public can reuse the
32 | material as expected. Licensors should clearly mark any
33 | material not subject to the license. This includes other CC-
34 | licensed material, or material used under an exception or
35 | limitation to copyright. More considerations for licensors:
36 | wiki.creativecommons.org/Considerations_for_licensors
37 |
38 | Considerations for the public: By using one of our public
39 | licenses, a licensor grants the public permission to use the
40 | licensed material under specified terms and conditions. If
41 | the licensor's permission is not necessary for any reason--for
42 | example, because of any applicable exception or limitation to
43 | copyright--then that use is not regulated by the license. Our
44 | licenses grant only permissions under copyright and certain
45 | other rights that a licensor has authority to grant. Use of
46 | the licensed material may still be restricted for other
47 | reasons, including because others have copyright or other
48 | rights in the material. A licensor may make special requests,
49 | such as asking that all changes be marked or described.
50 | Although not required by our licenses, you are encouraged to
51 | respect those requests where reasonable. More_considerations
52 | for the public:
53 | wiki.creativecommons.org/Considerations_for_licensees
54 |
55 | =======================================================================
56 |
57 | Creative Commons Attribution 4.0 International Public License
58 |
59 | By exercising the Licensed Rights (defined below), You accept and agree
60 | to be bound by the terms and conditions of this Creative Commons
61 | Attribution 4.0 International Public License ("Public License"). To the
62 | extent this Public License may be interpreted as a contract, You are
63 | granted the Licensed Rights in consideration of Your acceptance of
64 | these terms and conditions, and the Licensor grants You such rights in
65 | consideration of benefits the Licensor receives from making the
66 | Licensed Material available under these terms and conditions.
67 |
68 |
69 | Section 1 -- Definitions.
70 |
71 | a. Adapted Material means material subject to Copyright and Similar
72 | Rights that is derived from or based upon the Licensed Material
73 | and in which the Licensed Material is translated, altered,
74 | arranged, transformed, or otherwise modified in a manner requiring
75 | permission under the Copyright and Similar Rights held by the
76 | Licensor. For purposes of this Public License, where the Licensed
77 | Material is a musical work, performance, or sound recording,
78 | Adapted Material is always produced where the Licensed Material is
79 | synched in timed relation with a moving image.
80 |
81 | b. Adapter's License means the license You apply to Your Copyright
82 | and Similar Rights in Your contributions to Adapted Material in
83 | accordance with the terms and conditions of this Public License.
84 |
85 | c. Copyright and Similar Rights means copyright and/or similar rights
86 | closely related to copyright including, without limitation,
87 | performance, broadcast, sound recording, and Sui Generis Database
88 | Rights, without regard to how the rights are labeled or
89 | categorized. For purposes of this Public License, the rights
90 | specified in Section 2(b)(1)-(2) are not Copyright and Similar
91 | Rights.
92 |
93 | d. Effective Technological Measures means those measures that, in the
94 | absence of proper authority, may not be circumvented under laws
95 | fulfilling obligations under Article 11 of the WIPO Copyright
96 | Treaty adopted on December 20, 1996, and/or similar international
97 | agreements.
98 |
99 | e. Exceptions and Limitations means fair use, fair dealing, and/or
100 | any other exception or limitation to Copyright and Similar Rights
101 | that applies to Your use of the Licensed Material.
102 |
103 | f. Licensed Material means the artistic or literary work, database,
104 | or other material to which the Licensor applied this Public
105 | License.
106 |
107 | g. Licensed Rights means the rights granted to You subject to the
108 | terms and conditions of this Public License, which are limited to
109 | all Copyright and Similar Rights that apply to Your use of the
110 | Licensed Material and that the Licensor has authority to license.
111 |
112 | h. Licensor means the individual(s) or entity(ies) granting rights
113 | under this Public License.
114 |
115 | i. Share means to provide material to the public by any means or
116 | process that requires permission under the Licensed Rights, such
117 | as reproduction, public display, public performance, distribution,
118 | dissemination, communication, or importation, and to make material
119 | available to the public including in ways that members of the
120 | public may access the material from a place and at a time
121 | individually chosen by them.
122 |
123 | j. Sui Generis Database Rights means rights other than copyright
124 | resulting from Directive 96/9/EC of the European Parliament and of
125 | the Council of 11 March 1996 on the legal protection of databases,
126 | as amended and/or succeeded, as well as other essentially
127 | equivalent rights anywhere in the world.
128 |
129 | k. You means the individual or entity exercising the Licensed Rights
130 | under this Public License. Your has a corresponding meaning.
131 |
132 |
133 | Section 2 -- Scope.
134 |
135 | a. License grant.
136 |
137 | 1. Subject to the terms and conditions of this Public License,
138 | the Licensor hereby grants You a worldwide, royalty-free,
139 | non-sublicensable, non-exclusive, irrevocable license to
140 | exercise the Licensed Rights in the Licensed Material to:
141 |
142 | a. reproduce and Share the Licensed Material, in whole or
143 | in part; and
144 |
145 | b. produce, reproduce, and Share Adapted Material.
146 |
147 | 2. Exceptions and Limitations. For the avoidance of doubt, where
148 | Exceptions and Limitations apply to Your use, this Public
149 | License does not apply, and You do not need to comply with
150 | its terms and conditions.
151 |
152 | 3. Term. The term of this Public License is specified in Section
153 | 6(a).
154 |
155 | 4. Media and formats; technical modifications allowed. The
156 | Licensor authorizes You to exercise the Licensed Rights in
157 | all media and formats whether now known or hereafter created,
158 | and to make technical modifications necessary to do so. The
159 | Licensor waives and/or agrees not to assert any right or
160 | authority to forbid You from making technical modifications
161 | necessary to exercise the Licensed Rights, including
162 | technical modifications necessary to circumvent Effective
163 | Technological Measures. For purposes of this Public License,
164 | simply making modifications authorized by this Section 2(a)
165 | (4) never produces Adapted Material.
166 |
167 | 5. Downstream recipients.
168 |
169 | a. Offer from the Licensor -- Licensed Material. Every
170 | recipient of the Licensed Material automatically
171 | receives an offer from the Licensor to exercise the
172 | Licensed Rights under the terms and conditions of this
173 | Public License.
174 |
175 | b. No downstream restrictions. You may not offer or impose
176 | any additional or different terms or conditions on, or
177 | apply any Effective Technological Measures to, the
178 | Licensed Material if doing so restricts exercise of the
179 | Licensed Rights by any recipient of the Licensed
180 | Material.
181 |
182 | 6. No endorsement. Nothing in this Public License constitutes or
183 | may be construed as permission to assert or imply that You
184 | are, or that Your use of the Licensed Material is, connected
185 | with, or sponsored, endorsed, or granted official status by,
186 | the Licensor or others designated to receive attribution as
187 | provided in Section 3(a)(1)(A)(i).
188 |
189 | b. Other rights.
190 |
191 | 1. Moral rights, such as the right of integrity, are not
192 | licensed under this Public License, nor are publicity,
193 | privacy, and/or other similar personality rights; however, to
194 | the extent possible, the Licensor waives and/or agrees not to
195 | assert any such rights held by the Licensor to the limited
196 | extent necessary to allow You to exercise the Licensed
197 | Rights, but not otherwise.
198 |
199 | 2. Patent and trademark rights are not licensed under this
200 | Public License.
201 |
202 | 3. To the extent possible, the Licensor waives any right to
203 | collect royalties from You for the exercise of the Licensed
204 | Rights, whether directly or through a collecting society
205 | under any voluntary or waivable statutory or compulsory
206 | licensing scheme. In all other cases the Licensor expressly
207 | reserves any right to collect such royalties.
208 |
209 |
210 | Section 3 -- License Conditions.
211 |
212 | Your exercise of the Licensed Rights is expressly made subject to the
213 | following conditions.
214 |
215 | a. Attribution.
216 |
217 | 1. If You Share the Licensed Material (including in modified
218 | form), You must:
219 |
220 | a. retain the following if it is supplied by the Licensor
221 | with the Licensed Material:
222 |
223 | i. identification of the creator(s) of the Licensed
224 | Material and any others designated to receive
225 | attribution, in any reasonable manner requested by
226 | the Licensor (including by pseudonym if
227 | designated);
228 |
229 | ii. a copyright notice;
230 |
231 | iii. a notice that refers to this Public License;
232 |
233 | iv. a notice that refers to the disclaimer of
234 | warranties;
235 |
236 | v. a URI or hyperlink to the Licensed Material to the
237 | extent reasonably practicable;
238 |
239 | b. indicate if You modified the Licensed Material and
240 | retain an indication of any previous modifications; and
241 |
242 | c. indicate the Licensed Material is licensed under this
243 | Public License, and include the text of, or the URI or
244 | hyperlink to, this Public License.
245 |
246 | 2. You may satisfy the conditions in Section 3(a)(1) in any
247 | reasonable manner based on the medium, means, and context in
248 | which You Share the Licensed Material. For example, it may be
249 | reasonable to satisfy the conditions by providing a URI or
250 | hyperlink to a resource that includes the required
251 | information.
252 |
253 | 3. If requested by the Licensor, You must remove any of the
254 | information required by Section 3(a)(1)(A) to the extent
255 | reasonably practicable.
256 |
257 | 4. If You Share Adapted Material You produce, the Adapter's
258 | License You apply must not prevent recipients of the Adapted
259 | Material from complying with this Public License.
260 |
261 |
262 | Section 4 -- Sui Generis Database Rights.
263 |
264 | Where the Licensed Rights include Sui Generis Database Rights that
265 | apply to Your use of the Licensed Material:
266 |
267 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right
268 | to extract, reuse, reproduce, and Share all or a substantial
269 | portion of the contents of the database;
270 |
271 | b. if You include all or a substantial portion of the database
272 | contents in a database in which You have Sui Generis Database
273 | Rights, then the database in which You have Sui Generis Database
274 | Rights (but not its individual contents) is Adapted Material; and
275 |
276 | c. You must comply with the conditions in Section 3(a) if You Share
277 | all or a substantial portion of the contents of the database.
278 |
279 | For the avoidance of doubt, this Section 4 supplements and does not
280 | replace Your obligations under this Public License where the Licensed
281 | Rights include other Copyright and Similar Rights.
282 |
283 |
284 | Section 5 -- Disclaimer of Warranties and Limitation of Liability.
285 |
286 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
287 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
288 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
289 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
290 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
291 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
292 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
293 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
294 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
295 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
296 |
297 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
298 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
299 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
300 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
301 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
302 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
303 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
304 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
305 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
306 |
307 | c. The disclaimer of warranties and limitation of liability provided
308 | above shall be interpreted in a manner that, to the extent
309 | possible, most closely approximates an absolute disclaimer and
310 | waiver of all liability.
311 |
312 |
313 | Section 6 -- Term and Termination.
314 |
315 | a. This Public License applies for the term of the Copyright and
316 | Similar Rights licensed here. However, if You fail to comply with
317 | this Public License, then Your rights under this Public License
318 | terminate automatically.
319 |
320 | b. Where Your right to use the Licensed Material has terminated under
321 | Section 6(a), it reinstates:
322 |
323 | 1. automatically as of the date the violation is cured, provided
324 | it is cured within 30 days of Your discovery of the
325 | violation; or
326 |
327 | 2. upon express reinstatement by the Licensor.
328 |
329 | For the avoidance of doubt, this Section 6(b) does not affect any
330 | right the Licensor may have to seek remedies for Your violations
331 | of this Public License.
332 |
333 | c. For the avoidance of doubt, the Licensor may also offer the
334 | Licensed Material under separate terms or conditions or stop
335 | distributing the Licensed Material at any time; however, doing so
336 | will not terminate this Public License.
337 |
338 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
339 | License.
340 |
341 |
342 | Section 7 -- Other Terms and Conditions.
343 |
344 | a. The Licensor shall not be bound by any additional or different
345 | terms or conditions communicated by You unless expressly agreed.
346 |
347 | b. Any arrangements, understandings, or agreements regarding the
348 | Licensed Material not stated herein are separate from and
349 | independent of the terms and conditions of this Public License.
350 |
351 |
352 | Section 8 -- Interpretation.
353 |
354 | a. For the avoidance of doubt, this Public License does not, and
355 | shall not be interpreted to, reduce, limit, restrict, or impose
356 | conditions on any use of the Licensed Material that could lawfully
357 | be made without permission under this Public License.
358 |
359 | b. To the extent possible, if any provision of this Public License is
360 | deemed unenforceable, it shall be automatically reformed to the
361 | minimum extent necessary to make it enforceable. If the provision
362 | cannot be reformed, it shall be severed from this Public License
363 | without affecting the enforceability of the remaining terms and
364 | conditions.
365 |
366 | c. No term or condition of this Public License will be waived and no
367 | failure to comply consented to unless expressly agreed to by the
368 | Licensor.
369 |
370 | d. Nothing in this Public License constitutes or may be interpreted
371 | as a limitation upon, or waiver of, any privileges and immunities
372 | that apply to the Licensor or You, including from the legal
373 | processes of any jurisdiction or authority.
374 |
375 |
376 | =======================================================================
377 |
378 | Creative Commons is not a party to its public
379 | licenses. Notwithstanding, Creative Commons may elect to apply one of
380 | its public licenses to material it publishes and in those instances
381 | will be considered the “Licensor.” The text of the Creative Commons
382 | public licenses is dedicated to the public domain under the CC0 Public
383 | Domain Dedication. Except for the limited purpose of indicating that
384 | material is shared under a Creative Commons public license or as
385 | otherwise permitted by the Creative Commons policies published at
386 | creativecommons.org/policies, Creative Commons does not authorize the
387 | use of the trademark "Creative Commons" or any other trademark or logo
388 | of Creative Commons without its prior written consent including,
389 | without limitation, in connection with any unauthorized modifications
390 | to any of its public licenses or any other arrangements,
391 | understandings, or agreements concerning use of licensed material. For
392 | the avoidance of doubt, this paragraph does not form part of the
393 | public licenses.
394 |
395 | Creative Commons may be contacted at creativecommons.org.
--------------------------------------------------------------------------------
/site/articles/blitzjs/blitz-introduction-1.md:
--------------------------------------------------------------------------------
1 | # Blitz.js 入门教程:基于 Next.js 的下一代 React 全栈框架
2 |
3 | ## 译者序
4 |
5 | 苦 JS 生态久已。在 2020 年后,一直徘徊于该为自己构建怎样的技术栈,迟迟没有太多落地成果——库太多了,一个小场景就能有很多个解决方案;方向也太多了,哪怕大前端三个字,现今都能拆分为很多的细分领域。直到遇到了 Rome,看到社区已经开始尝试整合重构 Node 生态的前端工具链;又遇到了 Deno,直接摆脱 Node 的历史遗留问题来建设更贴近现代标准的 JavaScript/TypeScript 运行时;现在又遇到了 Blitz.js,一站式 React 全栈框架,在 Next.js 之上赋能更多的后端场景...于是自己的很多方向性问题都豁然开朗:通过建立不同的 Repo 来专攻不同的方向,且每个 Repo 都能有代表性的同时,覆盖更多的场景。于是——
6 |
7 | - [Deno 钻研之术](https://github.com/hylerrix/deno-tutorial):看未来,学标准。Node 也不落下。
8 | - [Deno 算法之旅](https://github.com/hylerrix/deno-algorithm):刷算法,玩测试。
9 | - [ECMAScript 面试宝典](https://github.com/hylerrix/es-interview):备面试,打基础;
10 | - [Blitz.js + React 全栈开发手册](https://github.com/hylerrix/blitzjs-tutorial):搞工程,尝全栈。深入 React,实战 Next.js,掌握后端开发。
11 | - 以及还有一切弃坑的 repo......
12 |
13 | 当然,Blitz.js 还有很多吸引人的特性:
14 |
15 | - **一体式全栈架构**:在一个 Monorepo 里从数据库到用户端全搞定,也没用重复性代码。试想前后端分离的架构下,如果你喜欢 TypeScript 的话,很可能得写两套相同 TS...且这种一体式架构很容易让自己的项目灵感从头到尾地快速落地。
16 | - **API 不再必须**:REST 和 GraphQL?或许都不需要,交给 Blitz.js 来在编译时构建。当需要提供 API 给更多端使用时,再结合相关库来生成 API。
17 | - **更轻松的开箱体验**:脚手架初始化后直接提供登录注册甚至重置密码功能,直接支持最基本的后端环境,开箱即用的体验不能再好,同时甚至能通过强大的 blitz generate CLI 快速植入生态圈中的主流库到项目中。
18 | - **并不会“学不动了”**:基于 Next.js,前端 React,后端 Prisma 等库,Blitz.js 构建于各种已经主流化的生态之上。
19 | - **拥抱未来**:Blitz.js 预计将于下个月(2021 年 4 月)发布 v1.0 版本。
20 |
21 | 本文属于《[Blitz.js + React 全栈开发手册](https://github.com/hylerrix/blitzjs-tutorial)》系列,原文翻译内容会同步更新到 [Blitz.js 中文仓库](https://github.com/blitz-js/zh-hans.blitzjs.com) 上。欢迎 Star 和 Watch:[https://github.com/hylerrix/blitzjs-tutorial](https://github.com/hylerrix/blitzjs-tutorial)。
22 |
23 | ## 快速入手
24 |
25 | ### 配置你的环境
26 |
27 | 你需要使用 Node 12 或者更新的版本。
28 |
29 | ### 安装 Blitz
30 |
31 | 执行 `npm install -g blitz`
32 |
33 | ### 创建一个新项目
34 |
35 | 1. `blitz new myAppName`
36 | 1. `cd myAppName`
37 | 1. `blitz dev`
38 | 1. 访问你的新项目 [http://localhost:3000](http://localhost:3000)
39 |
40 | ### 欢迎来到 Blitz 社区 👋
41 |
42 | Blitz 社区是个温暖、安全、多样化、包容也不失有趣的社区! LGBTQ+、女生和少数派欢迎你们的到来。
43 |
44 | [加入我们的 Discord 社区](https://discord.blitzjs.com),我们会在这里帮助每个人搭建 Blitz 应用。这里也是我们协作共建 Blitz 本身的重要场地。
45 |
46 | 对于提问以及会花费较长时间的讨论,[可以发帖到我们的论坛中](https://github.com/blitz-js/blitz/discussions)。
47 |
48 | 有关完整的介绍,请阅读 社区是如何运作的。文中包括了如何获得帮助、如何报告错误以及如何提出新功能建议等的详细指导。
49 |
50 | **欢迎你的帮助来使 Blitz 变得更好! 🤝**
51 |
52 | 我们有一个很棒的社区正在共同努力让 Blitz 成为世界上一流的框架。
53 |
54 | 你该如何提供帮助:
55 |
56 | 1. 通过 [在 GitHub 上提交 issue](https://github.com/blitz-js/blitz/issues/new/choose) 来反馈 Bug。
57 | 1. 贡献代码: 阅读贡献指南,以了解如何开始。
58 | 1. [赞助 & 捐赠](https://github.com/blitz-js/blitz#sponsors-and-donations),可以从 $5/月 开始。
59 | 1. 以及你觉得可以的其它任何方式!我们非常感谢你的任何贡献(如文档、视频、博客等)。如果你遇到任何阻碍,欢迎来 Discord 上与我们交流!:)
60 |
61 | ### 下一步
62 |
63 | #### 教程
64 |
65 | 教程篇 是有关 Blitz 所有基本内容的完整练习,其中包括将模型添加到数据库以及从前端读取和更新数据。
66 |
67 | #### 学习
68 |
69 | 这里有你想要熟悉的 Blitz 的关键概念:
70 |
71 | - 如何 新建页面
72 | - 如何 使用文件路由系统
73 | - 如何设置并 使用数据库
74 | - 如何使用 Blitz Queries 和 Mutations 来读写你的数据库。
75 | - 如何通过 `blitz generate` 命令来用脚手架生成数据库模型
76 | 的所有代码。
77 |
78 | ## 入门教程
79 |
80 | 在本教程中,我们将会指导你创建一个简易的投票系统。
81 |
82 | 我们将假设你已经 安装了 Blitz。你可以通过在终端运行以下命令来确定 Blitz 是否已经安装或检查安装的版本号:
83 |
84 | ```bash
85 | blitz -v
86 | ```
87 |
88 | 如果 Blitz 已经安装成功,你应该能看到安装的版本号。否则你会收到一条像这样的错误提示:“command not found: blitz”。
89 |
90 | ### 创建一个新应用
91 |
92 | 在命令行中,`cd` 进入你想要创建应用的根目录文件夹后,执行以下命令:
93 |
94 | ```sh
95 | blitz new my-blitz-app
96 | ```
97 |
98 | Blitz 会在你当前的文件夹中创建一个 `my-blitz-app` 文件夹。你接着会收到一个选择表单库的提示。本教程中将选择其中推荐的 `React Final Form` 库。
99 |
100 | 让我们看看 `blitz new` 命令创建了什么:
101 |
102 | ```markdown
103 | my-blitz-app
104 | ├── app/
105 | │ ├── api/
106 | │ ├── auth/
107 | │ │ ├── components/
108 | │ │ │ ├── LoginForm.tsx
109 | │ │ │ └── SignupForm.tsx
110 | │ │ ├── mutations/
111 | │ │ │ ├── changePassword.ts
112 | │ │ │ ├── forgotPassword.test.ts
113 | │ │ │ ├── forgotPassword.ts
114 | │ │ │ ├── login.ts
115 | │ │ │ ├── logout.ts
116 | │ │ │ ├── resetPassword.test.ts
117 | │ │ │ ├── resetPassword.ts
118 | │ │ │ └── signup.ts
119 | │ │ ├── pages/
120 | │ │ │ ├── forgot-password.tsx
121 | │ │ │ ├── login.tsx
122 | │ │ │ ├── reset-password.tsx
123 | │ │ │ └── signup.tsx
124 | │ │ └── validations.ts
125 | │ ├── core/
126 | │ │ ├── components/
127 | │ │ │ ├── Form.tsx
128 | │ │ │ └── LabeledTextField.tsx
129 | │ │ ├── hooks/
130 | │ │ │ └── useCurrentUser.ts
131 | │ │ └── layouts/
132 | │ │ └── Layout.tsx
133 | │ ├── pages/
134 | │ │ ├── 404.tsx
135 | │ │ ├── _app.tsx
136 | │ │ ├── _document.tsx
137 | │ │ ├── index.test.tsx
138 | │ │ └── index.tsx
139 | │ └── users/
140 | │ └── queries/
141 | │ └── getCurrentUser.ts
142 | ├── db/
143 | │ ├── index.ts
144 | │ ├── schema.prisma
145 | │ └── seeds.ts
146 | ├── integrations/
147 | ├── mailers/
148 | │ └── forgotPasswordMailer.ts
149 | ├── public/
150 | │ ├── favicon.ico*
151 | │ └── logo.png
152 | ├── test/
153 | │ ├── setup.ts
154 | │ └── utils.tsx
155 | ├── README.md
156 | ├── babel.config.js
157 | ├── blitz.config.js
158 | ├── jest.config.js
159 | ├── package.json
160 | ├── tsconfig.json
161 | ├── types.d.ts
162 | ├── types.ts
163 | └── yarn.lock
164 | ```
165 |
166 | 上述文件有:
167 |
168 | - `app/` 文件夹是项目中绝大多数功能的容器。你可以在这里放置任何页面或 API 路由。
169 | - `app/pages/` 文件夹是主要的页面文件夹。如果你使用过 Next.js 你将会立即注意到两者的不同。在 Blitz 中,你可以有许多 `pages` 文件夹,它们将在构建时合并在一起。
170 | - `app/core/` 文件夹是放置整个应用中使用到的通用组件、Hooks 等的主要位置。
171 | - `db/` 是数据库配置所在的位置。如果你正在编写模型或检查迁移情况,可以来这里。
172 | - `public/` 文件夹可以让你放置任何静态资源。如果你有要在应用中使用的图像、文件或视频等,都可以放置在其中。
173 | - `.babelrc.js`、`.env` 等(“dotfiles 文件”)是各种 JavaScript 工具需要用到的配置文件。
174 | - `blitz.config.js` 用于 Blitz 的高级自定义配置,与 `next.config.js` 相同
175 | - `tsconfig.json` 是我们推荐的 TypeScript 设置。
176 |
177 | ### 开发环境服务器
178 |
179 | 现在,如果你还没有在 `my-blitz-app` 文件夹下,确保切换到其中,并运行以下命令:
180 |
181 | ```sh
182 | blitz dev
183 | ```
184 |
185 | 你将会在命令行中看到如下输出:
186 |
187 | ```sh
188 | ✔ Compiled
189 | Loaded env from /private/tmp/my-blitz-app/.env
190 | warn - You have enabled experimental feature(s).
191 | warn - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use them at your own risk.
192 |
193 | ready - started server on 0.0.0.0:3000, url: http://localhost:3000
194 | info - Using external babel configuration from /my-blitz-app/babel.config.js
195 | event - compiled successfully
196 | ```
197 |
198 | 现在服务器已成功运行,浏览器中访问 [localhost:3000](http://localhost:3000)。你将会看到带有 Blitz logo 的欢迎页面。成功了!
199 |
200 | ### 以用户身份注册
201 |
202 | Bliz 应用让用户登录和注册开箱即用!现在让我们来尝试一下。点击 **注册** 按钮,输入任何电子邮件和密码,然后单击 **创建账户** 后,你将被重定向到用户主页,并在其中可以看到你的用户 `id` 和 `role`。
203 |
204 | 如果需要,你也可以尝试注销并重新登录。或在登录页面上单击 **忘记密码**,以尝试重置密码流程。
205 |
206 | ### 编写你的第一个页面
207 |
208 | 接下来让我们创建你的第一个页面。
209 |
210 | 打开 `app/pages/index.tsx` 文件然后替换掉 `Home` 组件的所有内容为这段代码:
211 |
212 | ```jsx
213 | //...
214 |
215 | const Home: BlitzPage = () => {
216 | return (
217 |