Update Thai translated for Getting started

This commit is contained in:
iphayao
2018-05-27 15:15:12 +07:00
parent 7dbf73fa8f
commit e363e06e76
9 changed files with 112 additions and 117 deletions

View File

@@ -28,12 +28,12 @@
</li>
<li>
<a href="/{{ page.lang }}/starter/basic-routing.html">
ราเตอร์เบื้องต้น
ส้นทางเบื้องต้น
</a>
</li>
<li>
<a href="/{{ page.lang }}/starter/static-files.html">
ไฟล์แบบสแตติก
บริการไฟล์คงที่
</a>
</li>
<li>

View File

@@ -1,7 +1,18 @@
@font-face {
font-family: 'TH SarabunNew';
src: url("../fonts/th/THSarabunNew.woff")
}
#description .description {
position: relative;
top: -5px;
font: 100 4.1em "TH SarabunPSK", "Helvetica Neue", "Open Sans", sans-serif;
font: 100 4.1em "TH SarabunNew", "Helvetica Neue", "Open Sans", sans-serif;
color: #aeaeae;
line-height: .87;
}
}
/* Non-English pages use pre and code tags for code blocks */
.non-en-doc .page pre[class*="language-"] {
padding-top: 10px !important;
padding-bottom: 10px !important;
}

BIN
fonts/th/THSarabunNew.woff Normal file

Binary file not shown.

View File

@@ -5,32 +5,32 @@ menu: starter
lang: th
---
# Basic routing
# เส้นทางเบื้องต้น
_Routing_ refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).
_เส้นทาง (Routing)_ เป็นการกำหนดการอ้างอิงว่าแอปพลิเคชันจะตอบสนองต่อคำร้องขอของเครื่องลูกข่ายที่มายังปลายทาง (endpoint) โดยเฉพาะได้อย่างไร ซึ่งเป็น URI (หรือ path) และวิธีการร้องขอ HTTP (GET, POST, และ อื่นๆ)
Each route can have one or more handler functions, which are executed when the route is matched.
แต่ละเส้นทางสามารถมีได้มากกว่าหนึ่งฟังชันส์จัดการ (handler function) ซึ่งสามารถดำเนินการเมื่อเส้นทางถูกจับคู่
Route definition takes the following structure:
การกำหนดเส้นทางใช้โครงสร้างดังนี้:
```js
app.METHOD(PATH, HANDLER)
```
Where:
เมื่อ:
- `app` is an instance of `express`.
- `METHOD` is an [HTTP request method](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods), in lowercase.
- `PATH` is a path on the server.
- `HANDLER` is the function executed when the route is matched.
- `app` เป็นอินสแตนซ์ของ `express`.
- `METHOD` เป็น [HTTP request method](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods), เป็นตัวพิมพ์เล็ก.
- `PATH` เป็นเส้นทางบนเซิร์ฟเวอร์.
- `HANDLER` เป็นฟังชันส์ที่กระทำเมื่อเส้นทางถูกจับคู่.
<div class="doc-box doc-notice" markdown="1">
This tutorial assumes that an instance of `express` named `app` is created and the server is running. If you are not familiar with creating an app and starting it, see the [Hello world example](/{{ page.lang }}/starter/hello-world.html).
การสอนนี้จะสมมติว่าอินสแตนซ์ของ `express` ชื่อว่า `app` จะถูกสร้างขึ้นเมื่อรันเซิร์ฟเวอร์ ถ้าไม่คุ้นเคยกับการสร้าง app และโครงสร้างของมัน ดูเพิ่มเติมได้ที่ [ตัวอย่าง Hello world](/{{ page.lang }}/starter/hello-world.html)
</div>
The following examples illustrate defining simple routes.
ตัวอย่างดังต่อไปนี้จะแสดงให้เห็นการกำหนดเส้นทางอย่างง่าย
Respond with `Hello World!` on the homepage:
ตอบสนองด้วยข้อความ `Hello World!` บนเพจหลัก:
```js
app.get('/', function (req, res) {
@@ -38,7 +38,7 @@ app.get('/', function (req, res) {
})
```
Respond to POST request on the root route (`/`), the application's home page:
ตอบสนองต่อการร้องขอด้วยวิธี POST บนเส้นทาง root (`/`) บนเพจหลักของแอปพลิเคชัน:
```js
app.post('/', function (req, res) {
@@ -46,7 +46,7 @@ app.post('/', function (req, res) {
})
```
Respond to a PUT request to the `/user` route:
ตอบสนองต่อการร้องขอด้วยวิธี PUT บนเส้นทาง `/user`:
```js
app.put('/user', function (req, res) {
@@ -54,12 +54,11 @@ app.put('/user', function (req, res) {
})
```
Respond to a DELETE request to the `/user` route:
ตอบสนองต่อการร้องขอด้วยวิธี DELETE บนเส้นทาง `/user`:
```js
app.delete('/user', function (req, res) {
res.send('Got a DELETE request at /user')
})
```
For more details about routing, see the [routing guide](/{{ page.lang }}/guide/routing.html).
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการกำหนดเส้นทาง ดูได้ที่ [คำแนะนำการกำหนดเส้นทาง](/{{ page.lang }}/guide/routing.html)

View File

@@ -5,59 +5,51 @@ menu: starter
lang: th
---
# FAQ
# คำถามที่พบบ่อย
## How should I structure my application?
## โครงสร้างแอปพลิเคชันของผมควรจะเป็นอย่าไร?
There is no definitive answer to this question. The answer depends
on the scale of your application and the team that is involved. To be as
flexible as possible, Express makes no assumptions in terms of structure.
ไม่มีการกำหนดนิยามของคำตอบสำหรับคำถามนี้ คำตอบขึ้นอยู่กับว่าขนาดของแอปพลิเคชันเท่าใด
และทีมงานที่ร่วมทำงานด้วยมีจำนวนขนาดไหน เพื่อที่จะทำงานคล่องตัวที่สุด Express ไม่มีข้อบังคับของโครงสร้าง
Routes and other application-specific logic can live in as many files
as you wish, in any directory structure you prefer. View the following
examples for inspiration:
เส้นทางและตรรกะเฉพาะแอปพลิเคชัน (application-specific) อื่นๆ สามารถทำงานในหลากหลายไฟล์ที่คุณต้องการ
ในหลายๆ ไดเรกเทอรีที่คุณต้องการ สามารถดูตัวอย่างด้านล่างนี้เพื่อเป็นแนวทาง:
* [Route listings](https://github.com/strongloop/express/blob/4.13.1/examples/route-separation/index.js#L32-47)
* [Route map](https://github.com/strongloop/express/blob/4.13.1/examples/route-map/index.js#L52-L66)
* [MVC style controllers](https://github.com/strongloop/express/tree/master/examples/mvc)
Also, there are third-party extensions for Express, which simplify some of these patterns:
ยังมีแหล่งตัวอย่างอื่นๆ สำหรับ Express ซึ่งทำให้ง่ายโดยใช้รูปแบบ:
* [Resourceful routing](https://github.com/expressjs/express-resource)
## How do I define models?
## จะกำหมดโมเกลอย่างไร?
Express has no notion of a database. This concept is
left up to third-party Node modules, allowing you to
interface with nearly any database.
Express ไม่มีความคิดของฐานข้อมูล แนวคิดนี้อยู่ในโมดูลอื่นของ Node ที่จะทำให้คุณติดต่อกับหลากหลายฐานข้อมูล
See [LoopBack](http://loopback.io) for an Express-based framework that is centered around models.
ดู [LoopBack](http://loopback.io) for an Express-based framework that is centered around models.
## How can I authenticate users?
## จะพิสูจน์ตัวตนของผู้ใช้งานได้อย่างไร?
Authentication is another opinionated area that Express does not
venture into. You may use any authentication scheme you wish.
For a simple username / password scheme, see [this example](https://github.com/expressjs/express/tree/master/examples/auth).
การพิสูจน์ตัวตนของผู้ใช้งานเป็นอีกส่วนหนี่งที่ Express ไม่ได้เข้าร่วม คุณอาจใช้รูปแบบพิสูจน์ตัวตนของผู้ใช้งานที่คุณต้องการ
สำหรับตัวอย่างที่ง่ายที่สุดคือรูปแบบ username / password, ดู [ตัวอย่างนี้](https://github.com/expressjs/express/tree/master/examples/auth)
## Which template engines does Express support?
## template engines ไหนบ้างที่ Express รองรับ?
Express supports any template engine that conforms with the `(path, locals, callback)` signature.
To normalize template engine interfaces and caching, see the
[consolidate.js](https://github.com/visionmedia/consolidate.js)
project for support. Unlisted template engines might still support the Express signature.
Express รองรับทุก template engine ที่สอดคล้องกับ `(path, locals, callback)`
เพื่อสร้างอินเทอร์เฟสสำหรับ template engine และการเคช ดูที่ [consolidate.js](https://github.com/visionmedia/consolidate.js)
โครงการที่รองรับ ที่ไม่อยู่ในรายการ template engines อาจยังคงรองรับโดย Express
For more information, see [Using template engines with Express](/{{page.lang}}/guide/using-template-engines.html).
สำหรับข้อมูลเพิ่มเติม, ดูที่ [การใช้ template engine กับ Express](/{{page.lang}}/guide/using-template-engines.html)
## How do I handle 404 responses?
## จะจัดการกับการตอบสนอง 404 ได้อย่างไร?
In Express, 404 responses are not the result of an error, so
the error-handler middleware will not capture them. This behavior is
because a 404 response simply indicates the absence of additional work to do;
in other words, Express has executed all middleware functions and routes,
and found that none of them responded. All you need to
do is add a middleware function at the very bottom of the stack (below all other functions)
to handle a 404 response:
ใน Express, การตอบสนอง 404 ไม่ใช้ผลของความผิดพลาด ดังนั้น
มิดเดิลแวร์ที่จัดการกับความผิดพลาด (error-handler) จะไม่ตรวจจับมัน พฤติกรรมเป็นแบบนี้
เพราะว่าการตอบสนอง 404 เป็นการบ่งชี้ว่ามีสิ่งผิดปรกติเกิดขึ้นอาจจะต้อมีสิ่งเพิ่มเติมมาจัดการ
ในงานอื่นๆ Express จะดำเนินการฟังก์ชันมิดเดิลแวร์และเส้นทางทั้งหมด และพบว่าไม่มีอะไรที่จะตอบสนอง
สิ่งที่คุณต้องทำคือเพิ่มฟังก์ชันมิดเดิลแวร์ที่ด้านล่างสุดเพื่อจัดการกับการตอบสนอง 404:
```js
app.use(function (req, res, next) {
@@ -65,10 +57,10 @@ app.use(function (req, res, next) {
})
```
## How do I setup an error handler?
## จะตั้งค่าจัดการความผิดพลาดได้อย่างไร?
You define error-handling middleware in the same way as other middleware,
except with four arguments instead of three; specifically with the signature `(err, req, res, next)`:
กำหนดมิดเดิลแวร์จัดการความผิดพลาด (error-handler) เช่นเดียวกับมิดเดิลแวร์อื่น ยกเว้นอาร์กิวเมนต์ 4 ตัวแทนที่จะเป็น 3 ตัว
ดังนี้ `(err, req, res, next)`
```js
app.use(function (err, req, res, next) {
@@ -77,11 +69,9 @@ app.use(function (err, req, res, next) {
})
```
For more information, see [Error handling](/{{ page.lang }}/guide/error-handling.html).
สำหรับข้อมูลเพิ่มเติม, ดูที่ [Error handling](/{{ page.lang }}/guide/error-handling.html).
## How do I render plain HTML?
## จะสร้าง HTML ธรรมดาได้อย่างไร?
You don't! There's no need to "render" HTML with the `res.render()` function.
If you have a specific file, use the `res.sendFile()` function.
If you are serving many assets from a directory, use the `express.static()`
middleware function.
คุณไม่ต้อง! ไม่จำเป็นต้องสร้าง HTML ด้วยฟังก์ชัน `res.render()`
ถ้าคุณมีไฟล์เฉพาะ ใช้ฟังก์ชัน `res.sendFile()` ถ้าคุณต้องการบริการหลายสินทรัพย์จากไดเรกเทอรี ใช้ฟังก์ชันมิดเดิลแวร์ `express.static()`

View File

@@ -5,17 +5,16 @@ menu: starter
lang: th
---
# Express application generator
# เครื่องมือสร้าง Express
Use the application generator tool, `express-generator`, to quickly create an application skeleton.
ใช้เครื่องมือสร้าง โดยพินพ์คำสั่ง `express-generator` เพื่อสร้างโครงสร้างหลักของแอปพลิเคชันอย่างรวดเร็ว
The `express-generator` package installs the `express` command-line tool. Use the following command to do so:
`express-generator` ติดตั้งแพ็กเกจไปยังชุดคำสั่ง `express` ใช้คำสั่งด้านล่างนี้เพื่อติดตั้ง:
```sh
$ npm install express-generator -g
```
Display the command options with the `-h` option:
แสดงตัวเลือกคำสั่งทั้งหมดด้วย `h`:
```sh
$ express -h
@@ -35,8 +34,7 @@ $ express -h
--git add .gitignore
-f, --force force on non-empty directory
```
For example, the following creates an Express app named _myapp_. The app will be created in a folder named _myapp_ in the current working directory and the view engine will be set to <a href="https://pugjs.org/" target="_blank" title="Pug documentation">Pug</a>:
สำหรับตัวอย่าง คำสั่งข้างล่างนี้เพื่อสร้าง Express app ที่ชื่อว่า _myapp_ โดยจะสร้างโฟล์เดอร์ชื่อ _myapp_ ในไดเรกเทอรีที่ใช้งานอยู่ และตั้ง view engine เป็น <a href="https://pugjs.org/" target="_blank" title="Pug documentation">Pug</a>:
```sh
$ express --view=pug myapp
@@ -59,29 +57,28 @@ $ express --view=pug myapp
create : myapp/bin
create : myapp/bin/www
```
Then install dependencies:
แล้วติดตั้งโมดูลเกี่ยวโยง (dependencies):
```sh
$ cd myapp
$ npm install
```
On MacOS or Linux, run the app with this command:
บน MacOS หรือ Linux รัน app ด้วยคำสั่งนี้:
```sh
$ DEBUG=myapp:* npm start
```
On Windows, use this command:
บน Windows ใช้คำสั่งนี้:
```sh
> set DEBUG=myapp:* & npm start
```
Then load `http://localhost:3000/` in your browser to access the app.
แล้วโหลด `http://localhost:3000/` ในเว็บเบราว์เซอร์ของคุณเพื่อเข้าถึง app
The generated app has the following directory structure:
หลังจากสร้าง app แล้วจะได้โครงสร้างไดเรกทอรีดังนี้:
```sh
.
@@ -106,5 +103,5 @@ The generated app has the following directory structure:
```
<div class="doc-box doc-info" markdown="1">
The app structure created by the generator is just one of many ways to structure Express apps. Feel free to use this structure or modify it to best suit your needs.
โครงสร้าง app ที่สร้างโดยเครื่องมือสร้างเป็นเพียงวิธีหนึ่งของอีกหลายวิธี ของการสร้างโครงสร้าง app ของ Express สามารถใช้โครงสร้างนี้ได้ หรือว่าจะแก้ไขเพื่อให้เหมาะสมที่สุดสำหรับความต้องการของคุณ
</div>

View File

@@ -5,10 +5,10 @@ menu: starter
lang: th
---
# Hello world example
# ตัวอย่าง Hello world
<div class="doc-box doc-info" markdown="1">
Embedded below is essentially the simplest Express app you can create. It is a single file app &mdash; _not_ what you'd get if you use the [Express generator](/{{ page.lang }}/starter/generator.html), which creates the scaffolding for a full app with numerous JavaScript files, Jade templates, and sub-directories for various purposes.
โค้ดด้านล่างนี้เป็นแอปพลิเคชัน Express จำเป็นแบบง่ายที่สุดที่คุณสามารถสร้างขึ้นได้ โดยเป็นไฟล์ app ไฟล์เดียว &mdash; _ไม่ใช้_ โค้ดที่ได้จาก [Express generator](/{{ page.lang }}/starter/generator.html) ที่สร้างโครงสร้างเริ่มต้นสำหรับแอปพลิเคชันตัวเต็มที่มีไฟล์ JavaScript มากมาย และไดเรทอรีย่อยสำหรับวัตถุประสงค์ต่างๆ
</div>
<script src="https://embed.runkit.com" data-element-id="hello-example" data-mode="endpoint" async defer></script>
@@ -21,31 +21,29 @@ app.get('/', (req, res) => res.send('Hello World!'))
app.listen(3000, () => console.log('Example app listening on port 3000!'))
</code></pre></div>
This app starts a server and listens on port 3000 for connections. The app responds with "Hello World!" for requests
to the root URL (`/`) or _route_. For every other path, it will respond with a **404 Not Found**.
app นี้จะเริ่มต้นเซิร์ฟเวอร์และเฝ้าตรวจสอบ (listen) การเชื่อมต่อที่พอร์ต 3000 โดยที่ app จะตอบสนองด้วงคำว่า "Hello World!" สำหรับการร้องขอ
มายัง root URL (`/`) หรือ _route_ แต่สำหรับ path อื่นๆ app จะตอบสนองด้วย **404 Not Found**
The example above is actually a working server: Go ahead and click on the URL shown. You'll get a response, with real-time logs on the page, and any changes you make will be reflected in real time. This is powered by [RunKit](https://runkit.com), which provides an interactive JavaScript playground connected to a complete Node environment that runs in your web browser.
Below are instructions for running the same app on your local machine.
ตัวอย่างด้านบนเป็นเซิร์ฟเวอร์ที่ทำงานได้จริง โดยกดไปที่ลิงค์ URL ที่แสดงอยู่จะเปิดหน้าเว็บใหม่และตอบสนองคำร้องขอของคุณอย่างทันที และสิ่งที่คุณแก้ไขจะเปลี่ยนแปลงที่หน้าเว็บ เพียงกดไปที่ลิงค์อีกครั้ง ได้รับการสนับสนุนจาก [RunKit](https://runkit.com) ซึ่งให้การเชื่อมต่อ interactive JavaScript playground สำหรับสิ่งแวดล้อมที่สมบูรณ์ของ Node บนเว็บเบราว์เซอร์ คำสั่งด้านล้างสำหรับรัน app เดียวกันบนเครื่องของคุณ
<div class="doc-box doc-info" markdown="1">
RunKit is a third-party service not affiliated with the Express project.
RunKit เป็นบริการของบริษัทอื่นที่ไม่ใช้หน่วยงานของโครงการ Express
</div>
### Running Locally
### รันบนเครื่องของคุณ
First create a directory named `myapp`, change to it and run `npm init`. Then install `express` as a dependency, as per the [installation guide](/{{ page.lang }}/starter/installing.html).
เริ่มต้นด้วยการสร้างไดเรกทอรีชื่อว่า `myapp` เปลี่ยนไปที่ไดเรกทอรีที่สร้างขึ้นแล้วใสคำสั่ง `npm init` แล้วติดตั้ง `express` และ dependency ต่างๆ ดัง[ขั้นตอนการติดตั้ง](/{{ page.lang }}/starter/installing.html)
In the `myapp` directory, create a file named `app.js` and copy in the code from the example above.
ในไดเรกทอรี `myapp` สร้างไฟล์ `app.js` ขึ้นมาและคัดลอกโค้ดจากตัวอย่างข้างบนมากใส่ในไฟล์
<div class="doc-box doc-notice" markdown="1">
The `req` (request) and `res` (response) are the exact same objects that Node provides, so you can invoke
`req.pipe()`, `req.on('data', callback)`, and anything else you would do without Express involved.
`req` (คำร้องขอ) และ `res` (คำตอบสนอง) เป็นอ็อบเจกต์เดียวกันที่จัดให้โดย Node ซึ่งคุณสมารถเรียกใช้ `req.pip()` `req.on('data', callback)` และอื่นๆ โดยไม่ต้องเรียกใช้ Express
</div>
Run the app with the following command:
รัน app ด้วยคำสั่งนี้:
```sh
$ node app.js
```
Then, load `http://localhost:3000/` in a browser to see the output.
แล้วโหลด `http://localhost:3000/` ในเว็บเบราว์เซอร์เพื่อดูผลลัพธ์

View File

@@ -5,43 +5,43 @@ menu: starter
lang: th
---
# Installing
# การติดตั้ง
Assuming you've already installed [Node.js](https://nodejs.org/), create a directory to hold your application, and make that your working directory.
สมมุติว่าคุณได้ติดตั้ง [Node.js](https://nodejs.org/) ในเครื่องคุณเรียบร้อยแล้ว ใช้คำสั่งข้างล่างนี้เพื่อสร้างไดเรกทอรีสำหรับเก็บแอปพลิเคชันของคุณ และใช้เป็นเก็บไฟล์ที่จะสร้างขึ้นต่อไป
```sh
$ mkdir myapp
$ cd myapp
```
Use the `npm init` command to create a `package.json` file for your application.
For more information on how `package.json` works, see [Specifics of npm's package.json handling](https://docs.npmjs.com/files/package.json).
ใช้คำสั่ง `npm init` เพื่อสร้างไฟล์ `package.json` สำหรับแอปพลิเคชันของคุณ
สำหรับข้อมูลเพิ่มเติมว่า `package.json` ทำงานอย่างไร สำมารถดูได้ที่ [Specifics of npm's package.json handling](https://docs.npmjs.com/files/package.json)
```sh
$ npm init
```
This command prompts you for a number of things, such as the name and version of your application.
For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:
หลังจากใส่คำสั่งนี้จะมีข้อความพร้อมรับให้คุณใส่ข้อมูลต่างๆ สำหรับการสร้างแอปพลิเคชัน เช่น ชื่อและรุ่นของแอปพลิเคชันของคุณ
สำหรับตอนนี้ คุณเพียงแต่กดปุ่ม ENTER เพื่อใช้ค่าเริ่มต้นของข้อมูลส่วนต่างๆ ยกเว้นข้อมูลดังต่อไปนี้
```sh
entry point: (index.js)
```
Enter `app.js`, or whatever you want the name of the main file to be. If you want it to be `index.js`, hit RETURN to accept the suggested default file name.
ใส่ `app.js` หรือชื่ออะไรก็ตามที่คุณต้องการสำหรับไฟล์หลักที่จะสร้าง ถ้าคุณต้องการใช้ค่าเริ่มต้นเป็น `index.js` เพียงกดปุ่ม ENTER เพื่อรับค่าเริ่มต้นของไฟล์หลักที่แนะนำ
Now install Express in the `myapp` directory and save it in the dependencies list. For example:
ต่อไปเป็นการติดตั้ง Express ในไดเรกเทอรี `myapp` และบันทึกไว้ในรายการเกี่ยวโยง (dependencies list) ตัวอย่างเช่น:
```sh
$ npm install express --save
```
To install Express temporarily and not add it to the dependencies list:
สามารถติดตั้ง Express ชั่วคราวได้โดยไม่ใส่ไว้ในรายการเกี่ยวโยง ด้วยคำสั่ง:
```sh
$ npm install express --no-save
```
<div class="doc-box doc-info" markdown="1">
By default with version npm 5.0+ npm install adds the module to the `dependencies` list in the `package.json` file; with earlier versions of npm, you must specify the `--save` option explicitly. Then, afterwards, running `npm install` in the app directory will automatically install modules in the dependencies list.
โดยค่าเริ่มต้นของ npm รุ่นที่ 5.0 ขึ้นไป คำสั่ง `npm install` เพิ่มโมดูลต่างๆ ไปที่รายการ `dependencies` ในไฟล์ `package.json` ถ้าเป็นรุ่นแรกๆ ของ npm คุณจำเป็นต้องใส่คำสั่ง `--save` ไปพร้อมกับคำสั่ง `npm install` ซึ่อหลังจากนี้ใช้คำสั่ง `npm install` ในไดเรกเทอรีของคุณ จะติดตั้งโมดูลที่อยู่ในรายการ dependencies โดยอัตโนมัติ
</div>

View File

@@ -5,26 +5,26 @@ menu: starter
lang: th
---
# Serving static files in Express
# ให้บริการไฟล์คงที่ใน Express
To serve static files such as images, CSS files, and JavaScript files, use the `express.static` built-in middleware function in Express.
เพื่อให้บริการไฟล์แบบคงที่อย่างเช่น ไฟล์รูปภาพ, ไฟล์ CSS, และไฟล์ JavaScript ใช้ฟังก์ชันของมิดเดิลแวร์ในตัว `express.static` ใน Express
The function signature is:
โครงสร้างของฟังก์ชันคือ:
```js
express.static(root, [options])
```
The `root` argument specifies the root directory from which to serve static assets.
For more information on the `options` argument, see [express.static](/{{page.lang}}/4x/api.html#express.static).
อาร์กิวเมนต์ `root` เป็นตัวกำหนดไดเรกเทอรีฐานราก ซึ่งให้บริการสินทรัพย์คงที่ (static assets)
สำหรับข้อมูลเพิ่มเติมของอาร์กิวเมนต์ `options` ดูได้ที่ [express.static](/{{page.lang}}/4x/api.html#express.static)
For example, use the following code to serve images, CSS files, and JavaScript files in a directory named `public`:
ตัวอย่างเช่น การใช้งานโค้ดข้างล่างนี้เพื่อให้บริการ ไฟล์รูปภาพ, ไฟล์ CSS, และไฟล์ JavaScript ในไดเรกเทอรีชื่อว่า `public`:
```js
app.use(express.static('public'))
```
Now, you can load the files that are in the `public` directory:
ตอนนี้คุณสามารถโหลดไฟล์ที่อยู่ในไดเรกเทอรี `public` ได้ดังนี้:
```plain-text
http://localhost:3000/images/kitten.jpg
@@ -35,28 +35,28 @@ http://localhost:3000/hello.html
```
<div class="doc-box doc-info">
Express looks up the files relative to the static directory, so the name of the static directory is not part of the URL.
Express จะมองหาไฟล์ที่เกี่ยวข้องกับไดเรกเทอรีคงที่ ดังนั้นชื่อของไดเรกเทอรีคงที่จะต้องไม่เป็นส่วนหนึ่งของ URL
</div>
To use multiple static assets directories, call the `express.static` middleware function multiple times:
เพื่อใช้สินทรัพย์คงที่หลายไดเรกเทอรี สามารถเรียกใช้มิดเดิลแวร์ฟังก์ชัน `express.static` ได้หลายครั้ง:
```js
app.use(express.static('public'))
app.use(express.static('files'))
```
Express looks up the files in the order in which you set the static directories with the `express.static` middleware function.
Express จะมองหาไฟล์ตามลำดับตามที่คุณตั้งค่าไดเรกเทอรี่คงที่ด้วยมิดเดิลแวร์ฟังก์ชัน `express.static`
<div class="doc-box doc-info" markdown="1">NOTE: For best results, [use a reverse proxy](/{{page.lang}}/advanced/best-practice-performance.html#use-a-reverse-proxy) cache to improve performance of serving static assets.
<div class="doc-box doc-info" markdown="1">**หมายเหตุ:** สำหรับผลที่ดีที่สุด, [ใช้พร็อกซี่ย้อนกลับ](/{{page.lang}}/advanced/best-practice-performance.html#use-a-reverse-proxy) แคซเพื่อเพิ่มประสิทธิภาพของการใช้บริการสินทรัพย์ไฟล์คงที่
</div>
To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the `express.static` function, [specify a mount path](/{{ page.lang }}/4x/api.html#app.use) for the static directory, as shown below:
เพื่อสร้างคำนำหน้าเส้นทางเสมือน (ที่ซึ่งเส้นทางไม่สามารถมายังที่อยู่ของไฟล์จริงในระบบได้) สำหรับไฟล์ที่ให้บริการโดยฟังก์ชัน `express.static` [ระบุเส้นทาง](/{{ page.lang }}/4x/api.html#app.use) สำหรับไดเรทเทอรีคงที่ ดังนี้:
```js
app.use('/static', express.static('public'))
```
Now, you can load the files that are in the `public` directory from the `/static` path prefix.
ตอนนี้คุณสามารถโหลดไฟล์ที่อยู่ในไดเรกเทอรี `public` จากคำนำหน้าเส้นทาง `/static`
```plain-text
http://localhost:3000/static/images/kitten.jpg
@@ -66,10 +66,10 @@ http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
```
However, the path that you provide to the `express.static` function is relative to the directory from where you launch your `node` process. If you run the express app from another directory, it's safer to use the absolute path of the directory that you want to serve:
อย่างไรก็ตาม เส้นทางที่คุณให้ไว้ในฟังก์ชัน `express.static` มีความสัมพันธ์กับไดเรกเทอรีจากที่ซึ่งคุณการบวนการรัน `node` ของคุณ ถ้าคุณรัน app จากไดเรกเทอรีอื่น มันจะปลอดภัยกว่าถ้าใช้เส้นทางจริงของไดเรกเทอรีที่คุณต้องการบริการไฟล์คงที่:
```js
app.use('/static', express.static(path.join(__dirname, 'public')))
```
For more details about the `serve-static` function and its options, see [serve-static](/{{page.lang}}/resources/middleware/serve-static.html).
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับฟังก์ชัน `serve-static` และตัวเลือก ดูได้ที่ [serve-static](/{{page.lang}}/resources/middleware/serve-static.html)