You learned nine ideas in the JavaScript Lab. They all ran inside a browser tab. Everything you wrote lived on one page. Now the same language goes to three new places. Node.js runs JavaScript with no web page. React builds a page from small pieces. Next.js puts both halves in one project. 你在 JavaScript 实验室学了九个概念。 它们都在浏览器标签页里运行。你写的东西都住在一个页面上。现在同一种语言要去三个新地方。 Node.js 不用网页也能运行 JavaScript。React 用小零件搭页面。 Next.js 把两半放进一个项目。 คุณเรียนเก้าแนวคิดใน JavaScript Lab ทั้งหมดรันในแท็บเบราว์เซอร์ ทุกอย่างที่คุณเขียนอยู่ในหน้าเดียว ตอนนี้ภาษาเดิมไปสามที่ใหม่ Node.js รัน JavaScript โดยไม่มีหน้าเว็บ React สร้างหน้าเว็บจากชิ้นเล็ก ๆ Next.js เอาสองส่วนมาไว้ในโปรเจกต์เดียว
.js file on a computer. No tab. No
window. No buttons. Only a terminal. This is how JavaScript reads files. It talks to a
database. It answers other computers.
.js 文件。没有标签页。没有窗口。没有按钮。只有终端。
JavaScript 就是这样读文件、连数据库、回答别的电脑。
.js บนคอมพิวเตอร์ ไม่มีแท็บ ไม่มีหน้าต่าง ไม่มีปุ่ม มีแค่เทอร์มินัล
JavaScript อ่านไฟล์แบบนี้ คุยกับฐานข้อมูล และตอบคอมพิวเตอร์เครื่องอื่น
- run: npm run build. It runs on a GitHub computer, not yours.
- run: npm run build。它跑在 GitHub 的电脑上,不是你的。
- run: npm run build มันรันบนคอมพิวเตอร์ของ GitHub ไม่ใช่ของคุณ<Student name="Nok" /> becomes finished HTML at build
time. <Counter /> is sent as JavaScript. It wakes up
when the visitor clicks.
<Student name="Nok" /> 在构建时变成写好的 HTML。
<Counter /> 以 JavaScript 发出去。访客一点它就醒过来。
<Student name="Nok" /> กลายเป็น HTML สำเร็จรูปตอน build
<Counter /> ถูกส่งไปเป็น JavaScript มันตื่นเมื่อผู้เข้าชมคลิกnpm run build writes the out folder.
app/about/page.js becomes out/about.html. Pages
serves that file. The Next.js server never starts.
npm run build 写出 out 文件夹。
app/about/page.js 变成 out/about.html。
Pages 就发这个文件。Next.js 的服务器永远不会启动。
npm run build เขียนโฟลเดอร์ out
app/about/page.js กลายเป็น out/about.html
Pages เสิร์ฟไฟล์นั้น เซิร์ฟเวอร์ Next.js ไม่เคยเริ่มทำงานEvery 🐙 line above says the same thing a different way. Only the browser half runs on GitHub Pages. The rest runs before it. Section 4 has the settings. 上面每一条 🐙 都在用不同说法讲同一件事。 在 GitHub Pages 上,只有浏览器那一半会跑。其余的都在它之前跑完。 第 4 节有具体设置。 ทุกบรรทัด 🐙 ข้างบนพูดเรื่องเดียวกันคนละแบบ บน GitHub Pages รันแค่ครึ่งที่เป็นเบราว์เซอร์ ที่เหลือรันก่อนหน้านั้น หัวข้อ 4 มีการตั้งค่า
You need one free download. Nothing else. Work in this order. Learn the words. Drive the machines. Then build a real website. 你只需要下载一个免费的东西。别的都不用。按这个顺序做。先学词。再驾驶机器。 然后做一个真网站。 คุณต้องดาวน์โหลดของฟรีอย่างเดียว ไม่ต้องอย่างอื่น ทำตามลำดับนี้ เรียนคำศัพท์ ขับเครื่องมือ แล้วสร้างเว็บไซต์จริง
🟨 Not finished with the browser? Go back to the JavaScript Lab first. This page uses all nine ideas from it.Thirty-one words. Each card shows its layer. Node, React or Next. You see these words in every job advert. You see them in every error message. Programmers say them in English everywhere. Tap say it to hear a word. Tap tell me more for a long answer.
三十一个词。每张卡片标出它属于哪一层:Node、React 还是 Next。这些词出现在每条招聘广告里。也出现在每条错误信息里。全世界的程序员都用英语说它们。点“say it”听一个词。点“tell me more”看详细解释。
สามสิบเอ็ดคำ การ์ดแต่ละใบบอกชั้นของมัน Node, React หรือ Next คุณเห็นคำเหล่านี้ในประกาศรับสมัครงานทุกใบ คุณเห็นมันในข้อความ error ทุกอัน โปรแกรมเมอร์พูดเป็นภาษาอังกฤษทุกที่ แตะ say it เพื่อฟังคำหนึ่ง แตะ tell me more เพื่ออ่านคำอธิบายยาว
Four Node machines. Three React machines. Two Next.js machines. Each machine shows real code on the left. It shows a result on the right. 四台 Node 机器。三台 React 机器。两台 Next.js 机器。 每台机器左边是真代码。右边是结果。 เครื่อง Node สี่เครื่อง React สามเครื่อง Next.js สองเครื่อง แต่ละเครื่องแสดงโค้ดจริงทางซ้าย และผลลัพธ์ทางขวา
Node is a program. You give it a command. You write a file called app dot js. You type node app dot js. Node reads the file. It runs every line. It prints your logs. Then it stops. There is no page. There is no window. The black box is a terminal.
Node 是一个程序。你给它一条命令。你写一个叫 app.js 的文件。你输入 node app.js。Node 读这个文件。它一行一行地运行。它打印你的日志。然后它停止。没有网页。没有窗口。那个黑框是终端。
Node คือโปรแกรม คุณให้คำสั่งกับมัน คุณเขียนไฟล์ชื่อ app.js คุณพิมพ์ node app.js Node อ่านไฟล์ มันรันทุกบรรทัด มันพิมพ์ log ของคุณ แล้วมันหยุด ไม่มีหน้าเว็บ ไม่มีหน้าต่าง กล่องดำคือเทอร์มินัล
require and not <script>?
There is no HTML file here. So there is no place for a script tag. Node asks for code
by name. os is a part inside Node already.
为什么用 require,不用 <script>?
这里没有 HTML 文件。所以没有地方放 script 标签。Node 按名字要代码。
os 是 Node 里已经有的一个零件。
ทำไมใช้ require ไม่ใช่ <script>?
ที่นี่ไม่มีไฟล์ HTML จึงไม่มีที่ใส่แท็ก script Node ขอโค้ดด้วยชื่อ
os เป็นชิ้นส่วนที่อยู่ใน Node อยู่แล้วNine steps. You start with nothing. You finish with a website on the internet. Every command has a copy button. Do them in order. Do not skip step zero. Step two takes a few minutes on slow internet. That is normal. At the end you get an address. You can send it to your family.
九步。你从零开始。最后得到一个在互联网上的网站。每条命令都有复制按钮。按顺序做。不要跳过第 0 步。网速慢的话第 2 步要几分钟。这很正常。最后你会得到一个网址。你可以发给家里人。
เก้าขั้นตอน คุณเริ่มจากศูนย์ คุณจบด้วยเว็บไซต์บนอินเทอร์เน็ต ทุกคำสั่งมีปุ่มคัดลอก ทำตามลำดับ อย่าข้ามขั้นตอนศูนย์ ขั้นตอนสองใช้เวลาไม่กี่นาทีถ้าเน็ตช้า นั่นปกติ ตอนจบคุณได้ที่อยู่เว็บ คุณส่งให้ครอบครัวได้
Go to nodejs.org. Download
the version marked LTS. LTS means long term support. It is the boring, safe one.
Install it. Then close your terminal. Open a new one. People forget the new terminal.
去 nodejs.org。下载标着 LTS 的版本。LTS 是长期支持版。
它是无聊但安全的那个。装好它。然后关掉终端。开一个新的。大家都会忘记开新终端。
ไปที่ nodejs.org ดาวน์โหลดเวอร์ชันที่เขียนว่า LTS LTS แปลว่ารองรับระยะยาว
มันคือตัวที่น่าเบื่อแต่ปลอดภัย ติดตั้งมัน แล้วปิดเทอร์มินัล เปิดอันใหม่ คนมักลืมเปิดเทอร์มินัลใหม่
Two version numbers means it works. Do you see command not found? Then the
install did not finish. Or you are still in the old terminal.
出现两个版本号就是成功了。看到 command not found 吗?
那就是没装完。或者你还在旧终端里。
ถ้าขึ้นเลขเวอร์ชันสองอัน แปลว่าใช้ได้ เห็น command not found ไหม?
แปลว่าติดตั้งไม่จบ หรือคุณยังอยู่ในเทอร์มินัลเดิม
One command builds the whole folder. It asks a few questions. Press Enter for every one.
Later you will know what they mean.
一条命令建好整个文件夹。它会问几个问题。每个都按回车。以后你就知道它们的意思了。
คำสั่งเดียวสร้างโฟลเดอร์ทั้งหมด มันถามไม่กี่ข้อ กด Enter ทุกข้อ ทีหลังคุณจะรู้ว่ามันหมายถึงอะไร
npm install keeps a package.
npx borrows it for one command. Then it throws it away. You need this tool
once. So you borrow it.
是 npx,不是 npm。npm install 把包留下来。
npx 借来用一条命令。然后扔掉。这个工具你只用一次。所以借就行。
npx ไม่ใช่ npm npm install เก็บแพ็กเกจไว้
npx ยืมมาใช้คำสั่งเดียว แล้วทิ้ง คุณใช้เครื่องมือนี้ครั้งเดียว จึงยืมพอOpen http://localhost:3000 in your browser. localhost means this
computer. Nobody on the internet can see it yet. Leave this terminal running. It is your
server. Press Ctrl+C to stop it.
在浏览器打开 http://localhost:3000。localhost 的意思是这台电脑。
互联网上还没人能看到。让这个终端开着。它就是你的服务器。按 Ctrl+C 停止。
เปิด http://localhost:3000 ในเบราว์เซอร์ localhost แปลว่าเครื่องนี้
ยังไม่มีใครบนอินเทอร์เน็ตเห็น ปล่อยเทอร์มินัลนี้ไว้ มันคือเซิร์ฟเวอร์ของคุณ กด Ctrl+C เพื่อหยุด
Open app/page.js. Delete everything in it. Paste this in. Save the file. Now
look at the browser. Do not press reload. It changed already.
打开 app/page.js。把里面全删掉。粘贴这段。保存文件。
现在看浏览器。不要按刷新。它已经变了。
เปิด app/page.js ลบทุกอย่างข้างใน วางโค้ดนี้ บันทึกไฟล์
แล้วดูเบราว์เซอร์ อย่ากดรีโหลด มันเปลี่ยนไปแล้ว
Make a folder app/about. Put a file page.js inside it. You wrote
no routing code. Now http://localhost:3000/about works.
建一个文件夹 app/about。在里面放一个 page.js。
你没写一行路由代码。现在 http://localhost:3000/about 就能用了。
สร้างโฟลเดอร์ app/about ใส่ไฟล์ page.js ข้างใน
คุณไม่ได้เขียนโค้ด routing เลย ตอนนี้ http://localhost:3000/about ใช้ได้แล้ว
Make a folder app/components. Put this file in it. One function, two
different results. This is machine 6 in a real project.
建一个 app/components 文件夹。把这个文件放进去。
一个函数,两个不同的结果。这就是第 6 台机器在真项目里的样子。
สร้างโฟลเดอร์ app/components ใส่ไฟล์นี้ลงไป
ฟังก์ชันเดียว ผลลัพธ์สองแบบ นี่คือเครื่องที่ 6 ในโปรเจกต์จริง
Then use it in app/page.js:
然后在 app/page.js 里用它:
แล้วใช้มันใน app/page.js:
This one needs state. So it must be a client component. Leave off the first line. Next.js
shows you an error. The error tells you why. That error is a good teacher.
这个需要 state。所以它必须是客户端组件。试试不写第一行。
Next.js 会给你一条错误。错误会告诉你为什么。那条错误是个好老师。
อันนี้ต้องใช้ state มันจึงต้องเป็น client component ลองไม่ใส่บรรทัดแรก
Next.js จะขึ้น error error บอกคุณว่าทำไม error นั้นเป็นครูที่ดี
A file called route.js is not a page. It is an address. It answers with data,
not HTML. This is the Node half of your project.
叫 route.js 的文件不是页面。它是一个网址。
它用数据回答,不用 HTML。这是你项目里 Node 的那一半。
ไฟล์ชื่อ route.js ไม่ใช่หน้าเว็บ มันคือที่อยู่
มันตอบด้วยข้อมูล ไม่ใช่ HTML นี่คือส่วน Node ของโปรเจกต์คุณ
Visit http://localhost:3000/api/temp. You see JSON in the browser. Now read
it from a server component. You need no useEffect. You need no loading
spinner. You only need await:
访问 http://localhost:3000/api/temp。浏览器里能看到 JSON。
现在从服务器组件读它。不需要 useEffect。不需要加载动画。只需要 await:
เข้า http://localhost:3000/api/temp คุณเห็น JSON ในเบราว์เซอร์
ทีนี้อ่านมันจาก server component ไม่ต้องใช้ useEffect ไม่ต้องมีวงหมุนรอโหลด
ใช้แค่ await:
Do you want more about APIs? The APIs lesson covers requests, responses and JSON. 想更多了解 API 吗?API 课讲请求、响应和 JSON。 อยากรู้เรื่อง API เพิ่มไหม? บทเรียน API อธิบาย request, response และ JSON
npm run dev is for you. It is slow on purpose. It reloads on every save.
npm run build makes the fast version. Visitors get that one. Always build
before you deploy. Build finds mistakes. Dev mode forgives them.
npm run dev 是给你用的。它故意做得慢。它每次保存都重新加载。
npm run build 做出快的版本。访客拿到的是那个。部署前一定先 build。
build 会找出错误。dev 模式会放过它们。
npm run dev มีไว้ให้คุณ มันช้าโดยตั้งใจ มันรีโหลดทุกครั้งที่บันทึก
npm run build สร้างเวอร์ชันเร็ว ผู้เข้าชมได้อันนั้น build ก่อน deploy เสมอ
build หาข้อผิดพลาดเจอ โหมด dev ปล่อยผ่าน
Push the project to GitHub. Then go to vercel.com. Sign in with GitHub. Import the repository. Vercel made
Next.js. So it needs no settings from you. Wait about one minute. You get a real address.
把项目推到 GitHub。然后去 vercel.com。用 GitHub 登录。导入仓库。
Vercel 就是 Next.js 的作者。所以不需要你做设置。等大约一分钟。你就有一个真网址了。
push โปรเจกต์ขึ้น GitHub แล้วไปที่ vercel.com ล็อกอินด้วย GitHub
import repository Vercel เป็นคนทำ Next.js จึงไม่ต้องตั้งค่าอะไร รอประมาณหนึ่งนาที คุณได้ที่อยู่จริง
node_modules or .env.
create-next-app already wrote a .gitignore. It keeps both out.
Do not delete it. Robots look for secret keys on GitHub. They find them in minutes.
node_modules 或 .env。
create-next-app 已经写好了 .gitignore。它把这两样挡住。
不要删它。机器人在 GitHub 上找密钥。几分钟就能找到。
node_modules หรือ .env
create-next-app เขียน .gitignore ไว้แล้ว มันกันทั้งสองอย่าง
อย่าลบทิ้ง บอทหากุญแจลับบน GitHub มันเจอในไม่กี่นาทีNew to git? Start with the GitHub lesson. Then come back to this step. 还不会 git?先看 GitHub 课。然后回到这一步。 ยังไม่เคยใช้ git? เริ่มที่ บทเรียน GitHub แล้วกลับมาขั้นตอนนี้
Vercel is not the only way. GitHub can host it free. But GitHub Pages runs no server. Read the next section first. Vercel 不是唯一的办法。GitHub 也能免费帮你托管。 但 GitHub Pages 不跑服务器。先读下一节。 Vercel ไม่ใช่ทางเดียว GitHub ก็โฮสต์ฟรีได้ แต่ GitHub Pages ไม่รันเซิร์ฟเวอร์ อ่านหัวข้อถัดไป ก่อน
GitHub Pages is free hosting. It only serves files. It runs no Node. So your pages still work. Your API routes do not. You change three settings. You add one empty file. Then GitHub builds the site for you. Click a feature below. See if it works there.
GitHub Pages 是免费托管。它只提供文件。它不跑 Node。所以你的页面还能用。你的 API 路由不能用。你要改三个设置。你要加一个空文件。然后 GitHub 帮你构建网站。点下面一个功能。看看它在那里能不能用。
GitHub Pages คือโฮสต์ฟรี มันเสิร์ฟแค่ไฟล์ มันไม่รัน Node หน้าของคุณยังใช้ได้ แต่ API route ใช้ไม่ได้ คุณเปลี่ยนสามค่า คุณเพิ่มไฟล์เปล่าหนึ่งไฟล์ แล้ว GitHub จะบิลด์เว็บให้คุณ คลิกฟีเจอร์ข้างล่าง ดูว่ามันใช้ได้ไหม
Vercel runs your project on a server. GitHub Pages does not. Pages is a shelf for finished files. Node still builds those files. It builds them on your computer. Or it builds them on GitHub. Then Pages hands them out. Vercel 在服务器上跑你的项目。GitHub Pages 不是。 Pages 是一个放成品文件的架子。Node 依然负责做出这些文件。 在你的电脑上做。或者在 GitHub 上做。然后 Pages 把它们发出去。 Vercel รันโปรเจกต์คุณบนเซิร์ฟเวอร์ GitHub Pages ไม่ทำ Pages คือชั้นวางไฟล์ที่เสร็จแล้ว Node ยังเป็นคนสร้างไฟล์เหล่านั้น สร้างบนคอมคุณ หรือสร้างบน GitHub แล้ว Pages ก็แจกมันออกไป
Make a file called next.config.js. Put this in it.
建一个叫 next.config.js 的文件。把这段放进去。
สร้างไฟล์ชื่อ next.config.js ใส่โค้ดนี้ลงไป
output: "export" makes plain files. Your site will live at
username.github.io/my-app. That folder name is the basePath.
Forget it and every link breaks.
output: "export" 让它生成普通文件。
你的网站会在 username.github.io/my-app。
那个文件夹名字就是 basePath。忘了它,所有链接都会断。
output: "export" ทำให้ได้ไฟล์ธรรมดา
เว็บคุณจะอยู่ที่ username.github.io/my-app
ชื่อโฟลเดอร์นั้นคือ basePath ลืมมัน ลิงก์จะพังหมด
_. Next.js puts everything
in _next. So your page loads with no colours and no buttons. One empty file
called .nojekyll stops this.
_ 开头的文件夹。Next.js 把东西都放在 _next 里。
所以你的页面打开后没有颜色,也没有按钮。一个叫 .nojekyll 的空文件能阻止这个。
_ Next.js เก็บทุกอย่างใน _next
หน้าคุณจึงโหลดมาไม่มีสีและไม่มีปุ่ม ไฟล์เปล่าชื่อ .nojekyll หยุดเรื่องนี้ได้You do not upload files by hand. You push your code. GitHub runs
npm run build for you. Then it puts the files on Pages. Save this file. Then
open Settings, then Pages. Set Source to GitHub Actions.
你不用手动上传文件。你只推代码。
GitHub 帮你跑 npm run build。然后把文件放到 Pages 上。
保存这个文件。然后打开 Settings,再点 Pages。
把 Source 设为 GitHub Actions。
คุณไม่ต้องอัปโหลดไฟล์เอง คุณแค่ push โค้ด
GitHub รัน npm run build ให้ แล้วเอาไฟล์ไปไว้ที่ Pages
บันทึกไฟล์นี้ แล้วเปิด Settings แล้ว Pages
ตั้ง Source เป็น GitHub Actions
Click a file or a feature. The box tells you the answer. This is machine 9 again. No
server means no server code.
点一个文件或功能。右边的框会告诉你答案。
这又是第 9 台机器。没有服务器,就没有服务器代码。
คลิกไฟล์หรือฟีเจอร์ กล่องจะบอกคำตอบ
นี่คือเครื่องที่ 9 อีกครั้ง ไม่มีเซิร์ฟเวอร์ ก็ไม่มีโค้ดเซิร์ฟเวอร์
No Next.js? Vite is the normal way to build React. It needs the same folder name. Vite
builds to dist, not out. Change that one word in the workflow.
不用 Next.js?Vite 是做 React 项目的常见工具。
它也需要同样的文件夹名字。Vite 构建到 dist,不是 out。
在工作流里改那一个词就行。
ไม่ใช้ Next.js? Vite คือวิธีปกติในการสร้าง React
มันต้องใช้ชื่อโฟลเดอร์เดียวกัน Vite บิลด์ไปที่ dist ไม่ใช่ out
เปลี่ยนคำนั้นคำเดียวใน workflow
The krueng.ai site you are reading is static too. No Node runs here. It is only files. 你正在看的 krueng.ai 也是静态的。这里没有 Node 在跑。只有文件。 เว็บ krueng.ai ที่คุณอ่านอยู่ก็เป็นสเตติก ที่นี่ไม่มี Node รัน มีแต่ไฟล์
package.json is the list.
node_modules is the pile. Share the list. Never share the pile.
npm 把别人的代码拿来。package.json 是清单。
node_modules 是那一堆。共享清单。永远不共享那一堆。
npm เอาโค้ดจากคนอื่นมา package.json คือรายการ
node_modules คือกอง แชร์รายการ อย่าแชร์กองcount = count + 1 does nothing. Call
setCount.
永远不要手动改 state。count = count + 1 什么都不做。
要调用 setCount。
อย่าเปลี่ยน state ด้วยมือ count = count + 1 ไม่ทำอะไร
ต้องเรียก setCountapp/about/page.js is
/about. You write no routing code.
在 Next.js 里,文件夹就是网址。app/about/page.js 就是
/about。你不用写路由代码。
ใน Next.js โฟลเดอร์คือที่อยู่ app/about/page.js คือ
/about คุณไม่ต้องเขียนโค้ด routing"use client" to the
smallest piece. That piece needs a click or a state.
先用服务器。需要时才用客户端。给最小的一块加 "use client"。
那一块需要点击或 state。
เซิร์ฟเวอร์ก่อน ใช้ client เมื่อจำเป็น ใส่ "use client" ให้ชิ้นที่เล็กที่สุด
ชิ้นนั้นต้องการการคลิกหรือ stateoutput: "export" and add .nojekyll.
GitHub Pages 只提供文件。它不跑 Node。
所以页面能用。API 路由不能用。设 output: "export",并加上 .nojekyll。
GitHub Pages เสิร์ฟแค่ไฟล์ มันไม่รัน Node
หน้าจึงใช้ได้ API route ใช้ไม่ได้ ตั้ง output: "export" และเพิ่ม .nojekyllThank you for learning with us today. Build one small thing. Put it on the internet. Send the address to somebody. See you in the next lab. 感谢今天和我们一起学习。做一个小东西。把它放到互联网上。把网址发给一个人。 下个实验室见。 ขอบคุณที่เรียนกับเราวันนี้ สร้างของเล็ก ๆ หนึ่งอย่าง เอาขึ้นอินเทอร์เน็ต ส่งที่อยู่ให้ใครสักคน แล้วเจอกันในแล็บหน้า