Gửi nhanh HTML

Để phục vụ các tệp tĩnh như hình ảnh, tệp CSS và tệp JavaScript, hãy sử dụng chức năng phần mềm trung gian tích hợp sẵn

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
78 trong Express

Show

Chữ ký hàm là

express.static(root, [options])

Đối số

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
79 chỉ định thư mục gốc để phục vụ nội dung tĩnh. Để biết thêm thông tin về đối số
http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
80, xem nhanh. tĩnh

Ví dụ: sử dụng mã sau để phân phát hình ảnh, tệp CSS và tệp JavaScript trong thư mục có tên

app.use(express.static('public'))
0

app.use(express.static('public'))

Bây giờ, bạn có thể tải các tệp trong thư mục

app.use(express.static('public'))
0

http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html

Express tra cứu các tệp liên quan đến thư mục tĩnh, vì vậy tên của thư mục tĩnh không phải là một phần của URL

Để sử dụng nhiều thư mục nội dung tĩnh, hãy gọi hàm phần mềm trung gian

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
78 nhiều lần

app.use(express.static('public'))
app.use(express.static('files'))

Express tra cứu các tệp theo thứ tự mà bạn đặt các thư mục tĩnh bằng chức năng phần mềm trung gian

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
78

GHI CHÚ. Để có kết quả tốt nhất, hãy sử dụng bộ nhớ cache proxy ngược để cải thiện hiệu suất cung cấp nội dung tĩnh

Để tạo tiền tố đường dẫn ảo (trong đó đường dẫn không thực sự tồn tại trong hệ thống tệp) cho các tệp được cung cấp bởi hàm

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
78, hãy chỉ định đường dẫn gắn kết cho thư mục tĩnh, như minh họa bên dưới

app.use('/static', express.static('public'))

Bây giờ, bạn có thể tải các tệp trong thư mục

app.use(express.static('public'))
0 từ tiền tố đường dẫn
app.use(express.static('public'))
6

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html

Tuy nhiên, đường dẫn mà bạn cung cấp cho hàm

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
78 có liên quan đến thư mục nơi bạn khởi chạy quy trình
app.use(express.static('public'))
8 của mình. Nếu bạn chạy ứng dụng cấp tốc từ một thư mục khác, sẽ an toàn hơn khi sử dụng đường dẫn tuyệt đối của thư mục mà bạn muốn phân phát

Tạo một ứng dụng Express. Hàm

app.use(express.static('public'))
158 là hàm cấp cao nhất được xuất bởi mô-đun
app.use(express.static('public'))
159

app.use(express.static('public'))
3

phương pháp

bày tỏ. json([tùy chọn])

Phần mềm trung gian này có sẵn trong Express v4. 16. 0 trở đi

Đây là một chức năng phần mềm trung gian tích hợp trong Express. Nó phân tích các yêu cầu đến bằng tải trọng JSON và dựa trên trình phân tích cú pháp nội dung

Trả về phần mềm trung gian chỉ phân tích cú pháp JSON và chỉ xem xét các yêu cầu có tiêu đề

app.use(express.static('public'))
160 khớp với tùy chọn
app.use(express.static('public'))
161. Trình phân tích cú pháp này chấp nhận mọi mã hóa Unicode của nội dung và hỗ trợ lạm phát tự động mã hóa
app.use(express.static('public'))
162 và
app.use(express.static('public'))
163

Đối tượng

app.use(express.static('public'))
164 mới chứa dữ liệu được phân tích cú pháp được điền vào đối tượng
app.use(express.static('public'))
165 sau phần mềm trung gian (i. e.
app.use(express.static('public'))
166) hoặc một đối tượng trống (
app.use(express.static('public'))
167) nếu không có nội dung để phân tích cú pháp,
app.use(express.static('public'))
160 không khớp hoặc xảy ra lỗi

Vì hình dạng của

app.use(express.static('public'))
166 dựa trên đầu vào do người dùng kiểm soát, nên tất cả các thuộc tính và giá trị trong đối tượng này đều không đáng tin cậy và phải được xác thực trước khi tin cậy. Ví dụ:
app.use(express.static('public'))
170 có thể bị lỗi theo nhiều cách, chẳng hạn như
app.use(express.static('public'))
171 có thể không có hoặc có thể không phải là một chuỗi và
app.use(express.static('public'))
172 có thể không phải là một hàm mà thay vào đó là một chuỗi hoặc đầu vào khác của người dùng

Bảng sau đây mô tả các thuộc tính của đối tượng tùy chọn

app.use(express.static('public'))
173

Thuộc tínhMô tảTypeDefault
app.use(express.static('public'))
174Cho phép hoặc vô hiệu hóa việc xử lý các phần tử (nén) xì hơi; . Boolean
app.use(express.static('public'))
175____1176Kiểm soát kích thước cơ thể yêu cầu tối đa. Nếu đây là một số, thì giá trị chỉ định số byte; . Hỗn hợp
app.use(express.static('public'))
177____1178Tùy chọn
app.use(express.static('public'))
178 được chuyển trực tiếp tới
app.use(express.static('public'))
180 dưới dạng đối số thứ hai. Bạn có thể tìm thêm thông tin về đối số này trong tài liệu MDN về JSON. phân tích cú pháp. Hàm
app.use(express.static('public'))
181
app.use(express.static('public'))
182Cho phép hoặc không cho phép chỉ chấp nhận mảng và đối tượng; . Boolean
app.use(express.static('public'))
175____1161Điều này được sử dụng để xác định loại phương tiện mà phần mềm trung gian sẽ phân tích cú pháp. Tùy chọn này có thể là một chuỗi, mảng các chuỗi hoặc một hàm. Nếu không phải là hàm, tùy chọn
app.use(express.static('public'))
161 được chuyển trực tiếp đến thư viện loại là và đây có thể là tên mở rộng (như
app.use(express.static('public'))
187), loại kịch câm (như
app.use(express.static('public'))
188) hoặc loại kịch câm có ký tự đại diện (như
app.use(express.static('public'))
189 hoặc
app.use(express.static('public'))
190). Nếu là một hàm, tùy chọn
app.use(express.static('public'))
161 được gọi là
app.use(express.static('public'))
192 và yêu cầu được phân tích cú pháp nếu nó trả về giá trị trung thực. Mixed
app.use(express.static('public'))
193
app.use(express.static('public'))
194Tùy chọn này, nếu được cung cấp, được gọi là
app.use(express.static('public'))
195, trong đó
app.use(express.static('public'))
196 là một
app.use(express.static('public'))
197 của nội dung yêu cầu thô và
app.use(express.static('public'))
198 là mã hóa của yêu cầu. Việc phân tích cú pháp có thể bị hủy bỏ bằng cách đưa ra lỗi. Chức năng____1199

bày tỏ. thô([tùy chọn])

Phần mềm trung gian này có sẵn trong Express v4. 17. 0 trở đi

Đây là một chức năng phần mềm trung gian tích hợp trong Express. Nó phân tích cú pháp tải trọng yêu cầu đến thành một

app.use(express.static('public'))
197 và dựa trên trình phân tích cú pháp nội dung

Trả về phần mềm trung gian phân tích cú pháp tất cả các nội dung dưới dạng

app.use(express.static('public'))
197 và chỉ xem xét các yêu cầu có tiêu đề
app.use(express.static('public'))
160 khớp với tùy chọn
app.use(express.static('public'))
161. Trình phân tích cú pháp này chấp nhận mọi mã hóa Unicode của nội dung và hỗ trợ lạm phát tự động mã hóa
app.use(express.static('public'))
162 và
app.use(express.static('public'))
163

Một

app.use(express.static('public'))
164
app.use(express.static('public'))
197 mới chứa dữ liệu được phân tích cú pháp được điền vào đối tượng
app.use(express.static('public'))
165 sau phần mềm trung gian (i. e.
app.use(express.static('public'))
166) hoặc một đối tượng trống (
app.use(express.static('public'))
167) nếu không có nội dung để phân tích cú pháp,
app.use(express.static('public'))
160 không khớp hoặc xảy ra lỗi

As

app.use(express.static('public'))
166’s shape is based on user-controlled input, all properties and values in this object are untrusted and should be validated before trusting. For example,
app.use('/static', express.static('public'))
713 may fail in multiple ways, for example stacking multiple parsers
app.use(express.static('public'))
166 may be from a different parser. Testing that
app.use(express.static('public'))
166 is a
app.use(express.static('public'))
197 before calling buffer methods is recommended

Bảng sau đây mô tả các thuộc tính của đối tượng tùy chọn

app.use(express.static('public'))
173

Thuộc tínhMô tảTypeDefault
app.use(express.static('public'))
174Cho phép hoặc vô hiệu hóa việc xử lý các phần tử (nén) xì hơi; . Boolean
app.use(express.static('public'))
175____1176Kiểm soát kích thước cơ thể yêu cầu tối đa. Nếu đây là một số, thì giá trị chỉ định số byte; . Hỗn hợp
app.use(express.static('public'))
177____1161Điều này được sử dụng để xác định loại phương tiện mà phần mềm trung gian sẽ phân tích cú pháp. Tùy chọn này có thể là một chuỗi, mảng các chuỗi hoặc một hàm. Nếu không phải là hàm, tùy chọn
app.use(express.static('public'))
161 được chuyển trực tiếp đến thư viện loại là và đây có thể là tên mở rộng (như
app.use('/static', express.static('public'))
724), loại kịch câm (như
app.use('/static', express.static('public'))
725) hoặc loại kịch câm có ký tự đại diện (như
app.use(express.static('public'))
189 hoặc
app.use('/static', express.static('public'))
727). Nếu là một hàm, tùy chọn
app.use(express.static('public'))
161 được gọi là
app.use(express.static('public'))
192 và yêu cầu được phân tích cú pháp nếu nó trả về giá trị trung thực. Mixed
app.use('/static', express.static('public'))
730
app.use(express.static('public'))
194Tùy chọn này, nếu được cung cấp, được gọi là
app.use(express.static('public'))
195, trong đó
app.use(express.static('public'))
196 là một
app.use(express.static('public'))
197 của nội dung yêu cầu thô và
app.use(express.static('public'))
198 là mã hóa của yêu cầu. Việc phân tích cú pháp có thể bị hủy bỏ bằng cách đưa ra lỗi. Function
app.use(express.static('public'))
199

bày tỏ. Bộ định tuyến([tùy chọn])

Tạo một đối tượng bộ định tuyến mới

app.use(express.static('public'))
1

Tham số

app.use(express.static('public'))
173 tùy chọn chỉ định hành vi của bộ định tuyến

PropertyDescriptionDefaultAvailability

app.use('/static', express.static('public'))
738Enable case sensitivity. Bị tắt theo mặc định, xử lý “/Foo” và “/foo” như nhau.
app.use('/static', express.static('public'))
739Preserve the
app.use('/static', express.static('public'))
740 values from the parent router. Nếu cha và con có tên tham số xung đột, giá trị của con sẽ được ưu tiên.
app.use('/static', express.static('public'))
7414. 5. 0+______1182Bật định tuyến nghiêm ngặt. Bị tắt theo mặc định, “/foo” và “/foo/” được bộ định tuyến xử lý như nhau

Bạn có thể thêm các tuyến phương thức HTTP và phần mềm trung gian (chẳng hạn như

app.use('/static', express.static('public'))
743,
app.use('/static', express.static('public'))
744,
app.use('/static', express.static('public'))
745, v.v.) vào
app.use('/static', express.static('public'))
746 giống như một ứng dụng

Để biết thêm thông tin, xem Bộ định tuyến

bày tỏ. tĩnh (gốc, [tùy chọn])

Đây là một chức năng phần mềm trung gian tích hợp trong Express. It serves static files and is based on serve-static

GHI CHÚ. Để có kết quả tốt nhất, hãy sử dụng bộ nhớ cache proxy ngược để cải thiện hiệu suất cung cấp nội dung tĩnh

Đối số

app.use('/static', express.static('public'))
747 chỉ định thư mục gốc để phục vụ nội dung tĩnh. Hàm xác định tệp sẽ phân phối bằng cách kết hợp
app.use('/static', express.static('public'))
748 với thư mục
app.use('/static', express.static('public'))
747 được cung cấp. Khi không tìm thấy tệp, thay vì gửi phản hồi 404, nó sẽ gọi
app.use('/static', express.static('public'))
750 để chuyển sang phần mềm trung gian tiếp theo, cho phép xếp chồng và dự phòng

Bảng sau mô tả các thuộc tính của đối tượng

app.use(express.static('public'))
173. Xem thêm ví dụ bên dưới

Thuộc tínhDescriptionTypeDefault
app.use('/static', express.static('public'))
752Xác định cách tệp dotfiles (tệp hoặc thư mục bắt đầu bằng dấu chấm “. ”) are treated

See dotfiles below

Chuỗi“bỏ qua”
app.use('/static', express.static('public'))
753Bật hoặc tắt tạo etag

GHI CHÚ.

app.use('/static', express.static('public'))
754 luôn gửi ETags yếu

Boolean
app.use(express.static('public'))
175____6756Đặt dự phòng cho phần mở rộng tệp. Nếu không tìm thấy tệp, hãy tìm tệp có phần mở rộng được chỉ định và cung cấp tệp đầu tiên được tìm thấy. Thí dụ.
app.use('/static', express.static('public'))
757. Mixed
app.use('/static', express.static('public'))
741
app.use('/static', express.static('public'))
759 Bỏ qua lỗi máy khách dưới dạng yêu cầu chưa được xử lý, nếu không thì chuyển tiếp lỗi máy khách

Xem dự phòng bên dưới

Boolean
app.use(express.static('public'))
175____6761Bật hoặc tắt chỉ thị
app.use('/static', express.static('public'))
761 trong tiêu đề phản hồi
app.use('/static', express.static('public'))
763. Nếu được bật, tùy chọn
app.use('/static', express.static('public'))
764 cũng phải được chỉ định để bật bộ nhớ đệm. Chỉ thị
app.use('/static', express.static('public'))
761 sẽ ngăn khách hàng được hỗ trợ thực hiện các yêu cầu có điều kiện trong suốt thời gian tồn tại của tùy chọn
app.use('/static', express.static('public'))
764 để kiểm tra xem tệp đã thay đổi chưa. Boolean
app.use('/static', express.static('public'))
741
app.use('/static', express.static('public'))
768Gửi tệp chỉ mục thư mục được chỉ định. Đặt thành
app.use('/static', express.static('public'))
741 để tắt lập chỉ mục thư mục. “chỉ số” hỗn hợp. html”
app.use('/static', express.static('public'))
770Đặt tiêu đề
app.use('/static', express.static('public'))
771 thành ngày sửa đổi cuối cùng của tệp trên HĐH. Boolean
app.use(express.static('public'))
175
app.use('/static', express.static('public'))
764Đặt thuộc tính max-age của tiêu đề Kiểm soát bộ đệm theo mili giây hoặc chuỗi ở định dạng ms. Number0
app.use('/static', express.static('public'))
774Chuyển hướng đến dấu “/” khi tên đường dẫn là một thư mục. Boolean
app.use(express.static('public'))
175____6776Chức năng đặt tiêu đề HTTP để phân phát cùng với tệp

Xem setHeaders bên dưới

Function

For more information, see Serving static files in Express. và Sử dụng phần mềm trung gian - Phần mềm trung gian tích hợp

dotfiles

Possible values for this option are

  • “allow” - No special treatment for dotfiles
  • “deny” - Deny a request for a dotfile, respond with
    app.use('/static', express.static('public'))
    
    777, then call
    app.use('/static', express.static('public'))
    
    750
  • “ignore” - Hành động như thể dotfile không tồn tại, trả lời bằng
    app.use('/static', express.static('public'))
    
    779, sau đó gọi
    app.use('/static', express.static('public'))
    
    750

NOTE. Với giá trị mặc định, nó sẽ không bỏ qua các tệp trong thư mục bắt đầu bằng dấu chấm

fallthrough

When this option is

app.use(express.static('public'))
175, client errors such as a bad request or a request to a non-existent file will cause this middleware to simply call
app.use('/static', express.static('public'))
750 to invoke the next middleware in the stack. When false, these errors (even 404s), will invoke
app.use('/static', express.static('public'))
783

Set this option to

app.use(express.static('public'))
175 so you can map multiple physical directories to the same web address or for routes to fill in non-existent files

Use

app.use('/static', express.static('public'))
741 if you have mounted this middleware at a path designed to be strictly a single file system directory, which allows for short-circuiting 404s for less overhead. This middleware will also reply to all methods

For this option, specify a function to set custom response headers. Alterations to the headers must occur synchronously

The signature of the function is

app.use('/static', express.static('public'))
7

Arguments

  • app.use('/static', express.static('public'))
    
    786, đối tượng phản hồi
  • app.use('/static', express.static('public'))
    
    787, the file path that is being sent
  • app.use('/static', express.static('public'))
    
    788, the
    app.use('/static', express.static('public'))
    
    788 object of the file that is being sent

Example of express. static

Dưới đây là một ví dụ về việc sử dụng chức năng phần mềm trung gian

app.use('/static', express.static('public'))
754 với một đối tượng tùy chọn phức tạp

app.use(express.static('public'))
47

express. văn bản([tùy chọn])

Phần mềm trung gian này có sẵn trong Express v4. 17. 0 trở đi

This is a built-in middleware function in Express. It parses incoming request payloads into a string and is based on body-parser

Trả về phần mềm trung gian phân tích cú pháp tất cả nội dung dưới dạng chuỗi và chỉ xem xét các yêu cầu có tiêu đề

app.use(express.static('public'))
160 khớp với tùy chọn
app.use(express.static('public'))
161. Trình phân tích cú pháp này chấp nhận mọi mã hóa Unicode của nội dung và hỗ trợ lạm phát tự động mã hóa
app.use(express.static('public'))
162 và
app.use(express.static('public'))
163

A new

app.use(express.static('public'))
164 string containing the parsed data is populated on the
app.use(express.static('public'))
165 object after the middleware (i. e.
app.use(express.static('public'))
166), or an empty object (
app.use(express.static('public'))
167) if there was no body to parse, the
app.use(express.static('public'))
160 was not matched, or an error occurred

As

app.use(express.static('public'))
166’s shape is based on user-controlled input, all properties and values in this object are untrusted and should be validated before trusting. For example,
app.use(express.static('public'))
4701 may fail in multiple ways, for example stacking multiple parsers
app.use(express.static('public'))
166 may be from a different parser. Testing that
app.use(express.static('public'))
166 is a string before calling string methods is recommended

Bảng sau đây mô tả các thuộc tính của đối tượng tùy chọn

app.use(express.static('public'))
173

PropertyDescriptionTypeDefault
app.use(express.static('public'))
4705Specify the default character set for the text content if the charset is not specified in the
app.use(express.static('public'))
160 header of the request. String
app.use(express.static('public'))
4707
app.use(express.static('public'))
174Enables or disables handling deflated (compressed) bodies; when disabled, deflated bodies are rejected. Boolean
app.use(express.static('public'))
175
app.use(express.static('public'))
176Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. Mixed
app.use(express.static('public'))
177
app.use(express.static('public'))
161This is used to determine what media type the middleware will parse. Tùy chọn này có thể là một chuỗi, mảng các chuỗi hoặc một hàm. If not a function,
app.use(express.static('public'))
161 option is passed directly to the type-is library and this can be an extension name (like
app.use(express.static('public'))
4714), a mime type (like
app.use(express.static('public'))
4715), or a mime type with a wildcard (like
app.use(express.static('public'))
189 or
app.use(express.static('public'))
4717). If a function, the
app.use(express.static('public'))
161 option is called as
app.use(express.static('public'))
192 and the request is parsed if it returns a truthy value. Mixed
app.use(express.static('public'))
4720
app.use(express.static('public'))
194Tùy chọn này, nếu được cung cấp, được gọi là
app.use(express.static('public'))
195, trong đó
app.use(express.static('public'))
196 là một
app.use(express.static('public'))
197 của nội dung yêu cầu thô và
app.use(express.static('public'))
198 là mã hóa của yêu cầu. The parsing can be aborted by throwing an error. Chức năng____1199

bày tỏ. được mã hóa url ([tùy chọn])

Phần mềm trung gian này có sẵn trong Express v4. 16. 0 trở đi

Đây là một chức năng phần mềm trung gian tích hợp trong Express. Nó phân tích các yêu cầu đến với tải trọng được mã hóa url và dựa trên trình phân tích cú pháp cơ thể

Trả về phần mềm trung gian chỉ phân tích các nội dung được mã hóa url và chỉ xem xét các yêu cầu có tiêu đề

app.use(express.static('public'))
160 khớp với tùy chọn
app.use(express.static('public'))
161. Trình phân tích cú pháp này chỉ chấp nhận mã hóa UTF-8 của nội dung và hỗ trợ lạm phát tự động mã hóa
app.use(express.static('public'))
162 và
app.use(express.static('public'))
163

Đối tượng

app.use(express.static('public'))
164 mới chứa dữ liệu được phân tích cú pháp được điền vào đối tượng
app.use(express.static('public'))
165 sau phần mềm trung gian (i. e.
app.use(express.static('public'))
166) hoặc một đối tượng trống (
app.use(express.static('public'))
167) nếu không có nội dung để phân tích cú pháp,
app.use(express.static('public'))
160 không khớp hoặc xảy ra lỗi. Đối tượng này sẽ chứa các cặp khóa-giá trị, trong đó giá trị có thể là một chuỗi hoặc mảng (khi
app.use(express.static('public'))
4736 là
app.use('/static', express.static('public'))
741) hoặc bất kỳ loại nào (khi
app.use(express.static('public'))
4736 là
app.use(express.static('public'))
175)

Vì hình dạng của

app.use(express.static('public'))
166 dựa trên đầu vào do người dùng kiểm soát, nên tất cả các thuộc tính và giá trị trong đối tượng này đều không đáng tin cậy và phải được xác thực trước khi tin cậy. Ví dụ:
app.use(express.static('public'))
170 có thể bị lỗi theo nhiều cách, chẳng hạn như
app.use(express.static('public'))
171 có thể không có hoặc có thể không phải là một chuỗi và
app.use(express.static('public'))
172 có thể không phải là một hàm mà thay vào đó là một chuỗi hoặc đầu vào khác của người dùng

Bảng sau đây mô tả các thuộc tính của đối tượng tùy chọn

app.use(express.static('public'))
173

PropertyDescriptionTypeDefault
app.use(express.static('public'))
4736Tùy chọn này cho phép chọn giữa phân tích cú pháp dữ liệu được mã hóa URL bằng thư viện
app.use(express.static('public'))
4746 (khi
app.use('/static', express.static('public'))
741) hoặc thư viện
app.use(express.static('public'))
4748 (khi
app.use(express.static('public'))
175). Cú pháp “mở rộng” cho phép các đối tượng và mảng phong phú được mã hóa thành định dạng được mã hóa URL, cho phép trải nghiệm giống như JSON với URL được mã hóa. Để biết thêm thông tin, vui lòng xem thư viện qs. Boolean
app.use(express.static('public'))
175____1174Cho phép hoặc vô hiệu hóa việc xử lý các phần tử (nén) xì hơi; . Boolean
app.use(express.static('public'))
175____1176Kiểm soát kích thước cơ thể yêu cầu tối đa. Nếu đây là một số, thì giá trị chỉ định số byte; . Mixed
app.use(express.static('public'))
177____14755Tùy chọn này kiểm soát số lượng tham số tối đa được phép trong dữ liệu được mã hóa URL. If a request contains more parameters than this value, an error will be raised. Số____14756____1161Số này được sử dụng để xác định loại phương tiện mà phần mềm trung gian sẽ phân tích cú pháp. Tùy chọn này có thể là một chuỗi, mảng các chuỗi hoặc một hàm. If not a function,
app.use(express.static('public'))
161 option is passed directly to the type-is library and this can be an extension name (like
app.use(express.static('public'))
4759), a mime type (like
app.use(express.static('public'))
4760), or a mime type with a wildcard (like
app.use(express.static('public'))
4761). If a function, the
app.use(express.static('public'))
161 option is called as
app.use(express.static('public'))
192 and the request is parsed if it returns a truthy value. Mixed
app.use(express.static('public'))
4764
app.use(express.static('public'))
194This option, if supplied, is called as
app.use(express.static('public'))
195, where
app.use(express.static('public'))
196 is a
app.use(express.static('public'))
197 of the raw request body and
app.use(express.static('public'))
198 is the encoding of the request. Việc phân tích cú pháp có thể bị hủy bỏ bằng cách đưa ra lỗi. Function
app.use(express.static('public'))
199

Application

The

app.use(express.static('public'))
4771 object conventionally denotes the Express application. Create it by calling the top-level
app.use(express.static('public'))
158 function exported by the Express module

app.use(express.static('public'))
30

The

app.use(express.static('public'))
4771 object has methods for

It also has settings (properties) that affect how the application behaves; for more information, see Application settings

Của cải

app. người dân địa phương

The

app.use(express.static('public'))
4774 object has properties that are local variables within the application, and will be available in templates rendered with res. render

app.use(express.static('public'))
33

Once set, the value of

app.use(express.static('public'))
4774 properties persist throughout the life of the application, in contrast with res. locals properties that are valid only for the lifetime of the request

Bạn có thể truy cập các biến cục bộ trong các mẫu được hiển thị trong ứng dụng. This is useful for providing helper functions to templates, as well as application-level data. Local variables are available in middleware via

app.use(express.static('public'))
4776 (see req. app)

app.use(express.static('public'))
36

app. mountpath

Thuộc tính

app.use(express.static('public'))
4777 chứa một hoặc nhiều mẫu đường dẫn mà ứng dụng phụ được gắn trên đó

Ứng dụng phụ là một phiên bản của

app.use(express.static('public'))
159 có thể được sử dụng để xử lý yêu cầu tới một tuyến đường

app.use(express.static('public'))
39

Nó tương tự như thuộc tính baseUrl của đối tượng

app.use(express.static('public'))
4779, ngoại trừ việc
app.use(express.static('public'))
4780 trả về đường dẫn URL phù hợp, thay vì các mẫu phù hợp

If a sub-app is mounted on multiple path patterns,

app.use(express.static('public'))
4777 returns the list of patterns it is mounted on, as shown in the following example

app.use(express.static('public'))
43

Events

ứng dụng. on('mount', callback(parent))

The

app.use(express.static('public'))
4782 event is fired on a sub-app, when it is mounted on a parent app. The parent app is passed to the callback function

GHI CHÚ

Sub-apps will

  • Not inherit the value of settings that have a default value. You must set the value in the sub-app
  • Kế thừa giá trị của cài đặt không có giá trị mặc định

For details, see Application settings

app.use(express.static('public'))
45

phương pháp

ứng dụng. tất cả (đường dẫn, gọi lại [, gọi lại. ])

This method is like the standard app. METHOD() methods, except it matches all HTTP verbs

Arguments

ArgumentDescriptionDefault____6787Đường dẫn mà chức năng phần mềm trung gian được gọi;
  • A string representing a path
  • Một mô hình đường dẫn
  • A regular expression pattern to match paths
  • An array of combinations of any of the above
For examples, see Path examples. '/' (root path)
app.use(express.static('public'))
4784Callback functions; can be
  • A middleware function
  • A series of middleware functions (separated by commas)
  • An array of middleware functions
  • Một sự kết hợp của tất cả những điều trên

Bạn có thể cung cấp nhiều chức năng gọi lại hoạt động giống như phần mềm trung gian, ngoại trừ việc các lệnh gọi lại này có thể gọi

app.use(express.static('public'))
4785 để bỏ qua (các) lệnh gọi lại tuyến đường còn lại. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there is no reason to proceed with the current route

Since router and app implement the middleware interface, you can use them as you would any other middleware function

For examples, see Middleware callback function examples

None

Examples

The following callback is executed for requests to

app.use(express.static('public'))
4786 whether using GET, POST, PUT, DELETE, or any other HTTP request method

app.use(express.static('public'))
10

The

app.use(express.static('public'))
4787 method is useful for mapping “global” logic for specific path prefixes or arbitrary matches. For example, if you put the following at the top of all other route definitions, it requires that all routes from that point on require authentication, and automatically load a user. Hãy nhớ rằng những cuộc gọi lại này không phải đóng vai trò là điểm cuối.
app.use(express.static('public'))
4788 can perform a task, then call
app.use('/static', express.static('public'))
750 to continue matching subsequent routes

app.use(express.static('public'))
11

Or the equivalent

app.use(express.static('public'))
12

Another example is white-listed “global” functionality. The example is similar to the ones above, but it only restricts paths that start with “/api”

app.use(express.static('public'))
13

ứng dụng. delete(path, callback [, callback . ])

Routes HTTP DELETE requests to the specified path with the specified callback functions. For more information, see the routing guide

Arguments

ArgumentDescriptionDefault____6787Đường dẫn mà chức năng phần mềm trung gian được gọi;
  • A string representing a path
  • Một mô hình đường dẫn
  • A regular expression pattern to match paths
  • An array of combinations of any of the above
For examples, see Path examples. '/' (root path)
app.use(express.static('public'))
4784Callback functions; can be
  • A middleware function
  • A series of middleware functions (separated by commas)
  • An array of middleware functions
  • Một sự kết hợp của tất cả những điều trên

Bạn có thể cung cấp nhiều chức năng gọi lại hoạt động giống như phần mềm trung gian, ngoại trừ việc các lệnh gọi lại này có thể gọi

app.use(express.static('public'))
4785 để bỏ qua (các) lệnh gọi lại tuyến đường còn lại. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there is no reason to proceed with the current route

Since router and app implement the middleware interface, you can use them as you would any other middleware function

For examples, see Middleware callback function examples

None

Example

app.use(express.static('public'))
14

ứng dụng. disable(name)

Đặt cài đặt Boolean

app.use(express.static('public'))
4793 thành
app.use('/static', express.static('public'))
741, trong đó
app.use(express.static('public'))
4793 là một trong các thuộc tính từ bảng cài đặt ứng dụng. Calling
app.use(express.static('public'))
4796 for a Boolean property is the same as calling
app.use(express.static('public'))
4797

For example

app.use(express.static('public'))
15

app. disabled(name)

Returns

app.use(express.static('public'))
175 if the Boolean setting
app.use(express.static('public'))
4793 is disabled (
app.use('/static', express.static('public'))
741), where
app.use(express.static('public'))
4793 is one of the properties from the app settings table

app.use(express.static('public'))
16

ứng dụng. kích hoạt (tên)

Đặt cài đặt Boolean

app.use(express.static('public'))
4793 thành
app.use(express.static('public'))
175, trong đó
app.use(express.static('public'))
4793 là một trong các thuộc tính từ bảng cài đặt ứng dụng. Gọi
app.use(express.static('public'))
3005 cho một thuộc tính Boolean cũng giống như gọi
app.use(express.static('public'))
3006

app.use(express.static('public'))
17

ứng dụng. đã bật (tên)

Returns

app.use(express.static('public'))
175 if the setting
app.use(express.static('public'))
4793 is enabled (
app.use(express.static('public'))
175), where
app.use(express.static('public'))
4793 is one of the properties from the app settings table

app.use(express.static('public'))
18

ứng dụng. engine(ext, callback)

Registers the given template engine

app.use(express.static('public'))
4784 as
app.use(express.static('public'))
3012

Theo mặc định, Express sẽ

app.use(express.static('public'))
3013 công cụ dựa trên phần mở rộng tệp. For example, if you try to render a “foo. pug” file, Express invokes the following internally, and caches the
app.use(express.static('public'))
3013 on subsequent calls to increase performance

app.use(express.static('public'))
19

Use this method for engines that do not provide

app.use(express.static('public'))
3015 out of the box, or if you wish to “map” a different extension to the template engine

For example, to map the EJS template engine to “. tập tin html”

app.use('/static', express.static('public'))
70

In this case, EJS provides a

app.use(express.static('public'))
3016 method with the same signature that Express expects.
app.use(express.static('public'))
3017, though note that it aliases this method as
app.use(express.static('public'))
3018 internally so if you’re using “. ejs” mà bạn không cần phải làm gì cả

Some template engines do not follow this convention. The consolidate. js ánh xạ các công cụ mẫu Node tuân theo quy ước này, vì vậy chúng hoạt động liền mạch với Express

app.use('/static', express.static('public'))
71

app. get(name)

Returns the value of

app.use(express.static('public'))
4793 app setting, where
app.use(express.static('public'))
4793 is one of the strings in the app settings table. Ví dụ

app.use('/static', express.static('public'))
72

app. get(đường dẫn, gọi lại [, gọi lại. ])

Routes HTTP GET requests to the specified path with the specified callback functions

Arguments

ArgumentDescriptionDefault____6787Đường dẫn mà chức năng phần mềm trung gian được gọi;
  • A string representing a path
  • Một mô hình đường dẫn
  • A regular expression pattern to match paths
  • An array of combinations of any of the above
For examples, see Path examples. '/' (root path)
app.use(express.static('public'))
4784Callback functions; can be
  • A middleware function
  • A series of middleware functions (separated by commas)
  • An array of middleware functions
  • Một sự kết hợp của tất cả những điều trên

Bạn có thể cung cấp nhiều chức năng gọi lại hoạt động giống như phần mềm trung gian, ngoại trừ việc các lệnh gọi lại này có thể gọi

app.use(express.static('public'))
4785 để bỏ qua (các) lệnh gọi lại tuyến đường còn lại. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there is no reason to proceed with the current route

Since router and app implement the middleware interface, you can use them as you would any other middleware function

For examples, see Middleware callback function examples

None

For more information, see the routing guide

Example

app.use('/static', express.static('public'))
73

ứng dụng. listen(path, [callback])

Starts a UNIX socket and listens for connections on the given path. Phương thức này giống hệt với http của Node. Server. listen()

app.use('/static', express.static('public'))
74

app. listen([port[, host[, backlog]]][, callback])

Binds and listens for connections on the specified host and port. Phương thức này giống hệt với http của Node. Server. nghe()

If port is omitted or is 0, the operating system will assign an arbitrary unused port, which is useful for cases like automated tasks (tests, etc. )

app.use('/static', express.static('public'))
75

The

app.use(express.static('public'))
4771 returned by
app.use(express.static('public'))
158 is in fact a JavaScript
app.use(express.static('public'))
3026, designed to be passed to Node’s HTTP servers as a callback to handle requests. This makes it easy to provide both HTTP and HTTPS versions of your app with the same code base, as the app does not inherit from these (it is simply a callback)

app.use('/static', express.static('public'))
76

The

app.use(express.static('public'))
3027 method returns an http. Server object and (for HTTP) is a convenience method for the following

app.use('/static', express.static('public'))
77

app. PHƯƠNG PHÁP (đường dẫn, gọi lại [, gọi lại. ])

Định tuyến một yêu cầu HTTP, trong đó METHOD là phương thức HTTP của yêu cầu, chẳng hạn như GET, PUT, POST, v.v., viết thường. Thus, the actual methods are

app.use(express.static('public'))
3028,
app.use(express.static('public'))
3029,
app.use(express.static('public'))
3030, and so on. See Routing methods below for the complete list

Arguments

ArgumentDescriptionDefault____6787Đường dẫn mà chức năng phần mềm trung gian được gọi;
  • A string representing a path
  • Một mô hình đường dẫn
  • A regular expression pattern to match paths
  • An array of combinations of any of the above
For examples, see Path examples. '/' (root path)
app.use(express.static('public'))
4784Callback functions; can be
  • A middleware function
  • A series of middleware functions (separated by commas)
  • An array of middleware functions
  • Một sự kết hợp của tất cả những điều trên

Bạn có thể cung cấp nhiều chức năng gọi lại hoạt động giống như phần mềm trung gian, ngoại trừ việc các lệnh gọi lại này có thể gọi

app.use(express.static('public'))
4785 để bỏ qua (các) lệnh gọi lại tuyến đường còn lại. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there is no reason to proceed with the current route

Since router and app implement the middleware interface, you can use them as you would any other middleware function

For examples, see Middleware callback function examples

None

Routing methods

Express hỗ trợ các phương thức định tuyến sau tương ứng với các phương thức HTTP cùng tên

  • app.use(express.static('public'))
    
    3034
  • app.use(express.static('public'))
    
    3035
  • app.use(express.static('public'))
    
    3036
  • app.use('/static', express.static('public'))
    
    743
  • app.use(express.static('public'))
    
    3038
  • app.use(express.static('public'))
    
    3039
  • app.use(express.static('public'))
    
    3040
  • app.use(express.static('public'))
    
    3041
  • app.use(express.static('public'))
    
    3042
  • app.use(express.static('public'))
    
    3043
  • app.use(express.static('public'))
    
    3044
  • app.use(express.static('public'))
    
    3045
  • app.use(express.static('public'))
    
    173
  • app.use(express.static('public'))
    
    3047
  • app.use('/static', express.static('public'))
    
    745
  • app.use(express.static('public'))
    
    3049
  • app.use('/static', express.static('public'))
    
    744
  • app.use(express.static('public'))
    
    3051
  • app.use(express.static('public'))
    
    3052
  • app.use(express.static('public'))
    
    3053
  • app.use(express.static('public'))
    
    3054
  • app.use(express.static('public'))
    
    3055
  • app.use(express.static('public'))
    
    3056

Tài liệu API chỉ có các mục nhập rõ ràng cho các phương thức HTTP phổ biến nhất là

app.use(express.static('public'))
3028,
app.use(express.static('public'))
3029,
app.use(express.static('public'))
3030 và
app.use(express.static('public'))
3060. However, the other methods listed above work in exactly the same way

To route methods that translate to invalid JavaScript variable names, use the bracket notation. For example,

app.use(express.static('public'))
3061

The

app.use(express.static('public'))
3028 function is automatically called for the HTTP
app.use(express.static('public'))
3063 method in addition to the
app.use(express.static('public'))
3064 method if
app.use(express.static('public'))
3065 was not called for the path before
app.use(express.static('public'))
3028

The method,

app.use(express.static('public'))
4787, is not derived from any HTTP method and loads middleware at the specified path for all HTTP request methods. Để biết thêm thông tin, hãy xem ứng dụng. all

For more information on routing, see the routing guide

app. param([name], callback)

Add callback triggers to route parameters, where

app.use(express.static('public'))
4793 is the name of the parameter or an array of them, and
app.use(express.static('public'))
4784 is the callback function. The parameters of the callback function are the request object, the response object, the next middleware, the value of the parameter and the name of the parameter, in that order

If

app.use(express.static('public'))
4793 is an array, the
app.use(express.static('public'))
4784 trigger is registered for each parameter declared in it, in the order in which they are declared. Furthermore, for each declared parameter except the last one, a call to
app.use(express.static('public'))
3072 inside the callback will call the callback for the next declared parameter. Đối với tham số cuối cùng, một lệnh gọi tới
app.use(express.static('public'))
3072 sẽ gọi phần mềm trung gian tiếp theo tại chỗ cho tuyến đường hiện đang được xử lý, giống như nó sẽ xảy ra nếu
app.use(express.static('public'))
4793 chỉ là một chuỗi

For example, when

app.use(express.static('public'))
3075 is present in a route path, you may map user loading logic to automatically provide
app.use(express.static('public'))
3076 to the route, or perform validations on the parameter input

app.use('/static', express.static('public'))
78

Param callback functions are local to the router on which they are defined. They are not inherited by mounted apps or routers. Hence, param callbacks defined on

app.use(express.static('public'))
4771 will be triggered only by route parameters defined on
app.use(express.static('public'))
4771 routes

All param callbacks will be called before any handler of any route in which the param occurs, and they will each be called only once in a request-response cycle, even if the parameter is matched in multiple routes, as shown in the following examples

app.use('/static', express.static('public'))
79

Trên

app.use(express.static('public'))
3079, phần sau được in

app.use(express.static('public'))
470
app.use(express.static('public'))
471

On

app.use(express.static('public'))
3080, the following is printed

app.use(express.static('public'))
472

The following section describes

app.use(express.static('public'))
3081, which is deprecated as of v4. 11. 0

Hành vi của phương thức

app.use(express.static('public'))
3082 có thể được thay đổi hoàn toàn bằng cách chỉ chuyển một hàm cho
app.use(express.static('public'))
3083. This function is a custom implementation of how
app.use(express.static('public'))
3082 should behave - it accepts two parameters and must return a middleware

The first parameter of this function is the name of the URL parameter that should be captured, the second parameter can be any JavaScript object which might be used for returning the middleware implementation

Phần mềm trung gian được trả về bởi hàm quyết định hành vi của những gì sẽ xảy ra khi tham số URL bị bắt

Trong ví dụ này, chữ ký

app.use(express.static('public'))
3082 được sửa đổi thành
app.use(express.static('public'))
3086. Thay vì chấp nhận tên và gọi lại, giờ đây,
app.use(express.static('public'))
3083 sẽ chấp nhận tên và số

app.use(express.static('public'))
473

Trong ví dụ này, chữ ký

app.use(express.static('public'))
3082 vẫn giữ nguyên, nhưng thay vì gọi lại phần mềm trung gian, chức năng kiểm tra loại dữ liệu tùy chỉnh đã được xác định để xác thực loại dữ liệu của id người dùng

app.use(express.static('public'))
474

Không thể sử dụng ký tự '______23089' để chụp một ký tự trong biểu thức chính quy chụp của bạn. Ví dụ: bạn không thể sử dụng

app.use(express.static('public'))
3090 để chụp
app.use(express.static('public'))
3091, thay vào đó hãy sử dụng
app.use(express.static('public'))
3092 hoặc
app.use(express.static('public'))
3093 (như trong
app.use(express.static('public'))
3094

ví dụ

app.use(express.static('public'))
475

ứng dụng. đường dẫn()

Trả về đường dẫn chính tắc của ứng dụng, một chuỗi

app.use(express.static('public'))
476

Hành vi của phương pháp này có thể trở nên rất phức tạp trong các trường hợp phức tạp của các ứng dụng được gắn kết. thường tốt hơn là sử dụng req. baseUrl để lấy đường dẫn chuẩn của ứng dụng

ứng dụng. bài đăng (đường dẫn, gọi lại [, gọi lại. ])

Định tuyến các yêu cầu HTTP POST đến đường dẫn đã chỉ định với các chức năng gọi lại được chỉ định. Để biết thêm thông tin, hãy xem hướng dẫn định tuyến

Arguments

ArgumentDescriptionDefault____6787Đường dẫn mà chức năng phần mềm trung gian được gọi;
  • A string representing a path
  • Một mô hình đường dẫn
  • A regular expression pattern to match paths
  • An array of combinations of any of the above
For examples, see Path examples. '/' (root path)
app.use(express.static('public'))
4784Callback functions; can be
  • A middleware function
  • A series of middleware functions (separated by commas)
  • An array of middleware functions
  • Một sự kết hợp của tất cả những điều trên

Bạn có thể cung cấp nhiều chức năng gọi lại hoạt động giống như phần mềm trung gian, ngoại trừ việc các lệnh gọi lại này có thể gọi

app.use(express.static('public'))
4785 để bỏ qua (các) lệnh gọi lại tuyến đường còn lại. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there is no reason to proceed with the current route

Since router and app implement the middleware interface, you can use them as you would any other middleware function

For examples, see Middleware callback function examples

None

Example

app.use(express.static('public'))
477

ứng dụng. đặt (đường dẫn, gọi lại [, gọi lại. ])

Định tuyến các yêu cầu HTTP PUT đến đường dẫn đã chỉ định với các chức năng gọi lại được chỉ định

Arguments

ArgumentDescriptionDefault____6787Đường dẫn mà chức năng phần mềm trung gian được gọi;
  • A string representing a path
  • Một mô hình đường dẫn
  • A regular expression pattern to match paths
  • An array of combinations of any of the above
For examples, see Path examples. '/' (root path)
app.use(express.static('public'))
4784Callback functions; can be
  • A middleware function
  • A series of middleware functions (separated by commas)
  • An array of middleware functions
  • Một sự kết hợp của tất cả những điều trên

Bạn có thể cung cấp nhiều chức năng gọi lại hoạt động giống như phần mềm trung gian, ngoại trừ việc các lệnh gọi lại này có thể gọi

app.use(express.static('public'))
4785 để bỏ qua (các) lệnh gọi lại tuyến đường còn lại. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there is no reason to proceed with the current route

Since router and app implement the middleware interface, you can use them as you would any other middleware function

For examples, see Middleware callback function examples

None

Example

app.use(express.static('public'))
478

ứng dụng. kết xuất (xem, [địa phương], gọi lại)

Trả về HTML được hiển thị của chế độ xem qua hàm

app.use(express.static('public'))
4784. Nó chấp nhận một tham số tùy chọn là một đối tượng chứa các biến cục bộ cho dạng xem. Nó giống như độ phân giải. render(), ngoại trừ nó không thể tự gửi chế độ xem được hiển thị tới máy khách

Hãy nghĩ về

app.use(express.static('public'))
3302 như một chức năng tiện ích để tạo các chuỗi xem được hiển thị. Nội bộ
app.use(express.static('public'))
3303 sử dụng
app.use(express.static('public'))
3302 để hiển thị chế độ xem

Biến cục bộ

app.use(express.static('public'))
3305 được dành riêng để bật chế độ xem bộ đệm. Đặt nó thành
app.use(express.static('public'))
175, nếu bạn muốn xem bộ đệm trong quá trình phát triển;

app.use(express.static('public'))
479

ứng dụng. lộ trình (đường đi)

Trả về một phiên bản của một tuyến đường, sau đó bạn có thể sử dụng tuyến đường này để xử lý các động từ HTTP bằng phần mềm trung gian tùy chọn. Sử dụng

app.use(express.static('public'))
3307 để tránh tên tuyến đường trùng lặp (và do đó là lỗi đánh máy)

app.use(express.static('public'))
300

ứng dụng. đặt (tên, giá trị)

Chỉ định cài đặt

app.use(express.static('public'))
4793 thành
app.use(express.static('public'))
3309. Bạn có thể lưu trữ bất kỳ giá trị nào bạn muốn, nhưng một số tên nhất định có thể được sử dụng để định cấu hình hành vi của máy chủ. Những tên đặc biệt này được liệt kê trong bảng cài đặt ứng dụng

Gọi

app.use(express.static('public'))
3005 cho một thuộc tính Boolean cũng giống như gọi
app.use(express.static('public'))
3006. Tương tự, gọi
app.use(express.static('public'))
4796 cho một thuộc tính Boolean cũng giống như gọi
app.use(express.static('public'))
4797

Truy xuất giá trị của một cài đặt với

app.use(express.static('public'))
3028

app.use(express.static('public'))
301

Cài đặt ứng dụng

Bảng sau liệt kê các cài đặt ứng dụng

Lưu ý rằng các ứng dụng phụ sẽ

  • Not inherit the value of settings that have a default value. You must set the value in the sub-app
  • Kế thừa giá trị của các cài đặt không có giá trị mặc định;

ngoại lệ. Các ứng dụng phụ sẽ kế thừa giá trị của

app.use(express.static('public'))
3315 mặc dù nó có giá trị mặc định (để tương thích ngược);

Loại thuộc tínhMô tảMặc định

app.use(express.static('public'))
3318

Boolean

Bật phân biệt chữ hoa chữ thường. Khi được bật, "/Foo" và "/foo" là các tuyến khác nhau. Khi bị tắt, "/Foo" và "/foo" được xử lý như nhau

GHI CHÚ. Các ứng dụng phụ sẽ kế thừa giá trị của cài đặt này

N/A (không xác định)

app.use(express.static('public'))
3319

Chế độ môi trường chuỗi. Đảm bảo đặt thành "sản xuất" trong môi trường sản xuất; . hiệu suất và độ tin cậy

app.use(express.static('public'))
3320 (biến môi trường
app.use(express.static('public'))
3317) hoặc “phát triển” nếu không đặt
app.use(express.static('public'))
3317

app.use('/static', express.static('public'))
753

đa dạng

Set the ETag response header. Để biết các giá trị có thể, hãy xem bảng tùy chọn

app.use('/static', express.static('public'))
753

Tìm hiểu thêm về tiêu đề HTTP ETag

app.use(express.static('public'))
3325

app.use(express.static('public'))
3326

StringChỉ định tên gọi lại JSONP mặc định

"gọi lại"

app.use(express.static('public'))
3327

Boolean

Cho phép thoát khỏi các phản hồi JSON từ các API

app.use(express.static('public'))
3328,
app.use(express.static('public'))
3329 và
app.use(express.static('public'))
3330. Điều này sẽ thoát khỏi các ký tự
app.use(express.static('public'))
3331,
app.use(express.static('public'))
3332 và
app.use(express.static('public'))
3333 dưới dạng các chuỗi thoát Unicode trong JSON. Mục đích của việc này là để hỗ trợ giảm thiểu một số kiểu tấn công XSS dai dẳng khi khách hàng đánh hơi phản hồi cho HTML

GHI CHÚ. Các ứng dụng phụ sẽ kế thừa giá trị của cài đặt này

N/A (không xác định)

app.use(express.static('public'))
3334

Đa dạngĐối số 'thay thế' được sử dụng bởi `JSON. xâu chuỗi`

GHI CHÚ. Các ứng dụng phụ sẽ kế thừa giá trị của cài đặt này

N/A (không xác định)

app.use(express.static('public'))
3335

Đa dạngĐối số 'không gian' được sử dụng bởi `JSON. xâu chuỗi`. Điều này thường được đặt thành số khoảng trắng sẽ sử dụng để thụt lề JSON

GHI CHÚ. Các ứng dụng phụ sẽ kế thừa giá trị của cài đặt này

N/A (không xác định)

app.use(express.static('public'))
3336

đa dạng

Vô hiệu hóa phân tích cú pháp truy vấn bằng cách đặt giá trị thành

app.use('/static', express.static('public'))
741 hoặc đặt trình phân tích cú pháp truy vấn để sử dụng "đơn giản" hoặc "mở rộng" hoặc chức năng phân tích cú pháp chuỗi truy vấn tùy chỉnh

Trình phân tích cú pháp truy vấn đơn giản dựa trên trình phân tích cú pháp truy vấn gốc của Node, chuỗi truy vấn

Trình phân tích cú pháp truy vấn mở rộng dựa trên qs

Hàm phân tích chuỗi truy vấn tùy chỉnh sẽ nhận chuỗi truy vấn hoàn chỉnh và phải trả về một đối tượng gồm các khóa truy vấn và giá trị của chúng

"mở rộng"

app.use(express.static('public'))
3338

Boolean

Kích hoạt định tuyến nghiêm ngặt. Khi được bật, bộ định tuyến coi "/foo" và "/foo/" là khác nhau. Mặt khác, bộ định tuyến coi "/foo" và "/foo/" giống nhau

GHI CHÚ. Các ứng dụng phụ sẽ kế thừa giá trị của cài đặt này

N/A (không xác định)

app.use(express.static('public'))
3339

NumberThe number of dot-separated parts of the host to remove to access subdomain. 2

app.use(express.static('public'))
3315

đa dạng

Cho biết ứng dụng đứng sau proxy mặt trước và sử dụng tiêu đề

app.use(express.static('public'))
3341 để xác định kết nối và địa chỉ IP của ứng dụng khách. NOTE.
app.use(express.static('public'))
3341 headers are easily spoofed and the detected IP addresses are unreliable

When enabled, Express attempts to determine the IP address of the client connected through the front-facing proxy, or series of proxies. The `req. ips` property, then contains an array of IP addresses the client is connected through. Để bật tính năng này, hãy sử dụng các giá trị được mô tả trong bảng tùy chọn proxy tin cậy

The `trust proxy` setting is implemented using the proxy-addr package. Để biết thêm thông tin, hãy xem tài liệu của nó

NOTE. Các ứng dụng phụ sẽ kế thừa giá trị của cài đặt này, mặc dù cài đặt này có giá trị mặc định

app.use('/static', express.static('public'))
741 (đã tắt)

app.use(express.static('public'))
3344

String hoặc ArrayMột thư mục hoặc một mảng các thư mục cho các dạng xem của ứng dụng. Nếu là một mảng, các dạng xem được tra cứu theo thứ tự xuất hiện trong mảng

app.use(express.static('public'))
3345

app.use(express.static('public'))
3316

Boolean

Cho phép xem bộ nhớ đệm tổng hợp mẫu

GHI CHÚ. Ứng dụng phụ sẽ không kế thừa giá trị của cài đặt này trong sản xuất (khi `NODE_ENV` là "sản xuất")

app.use(express.static('public'))
175 trong sản xuất, nếu không thì không xác định

app.use(express.static('public'))
3348

StringPhần mở rộng công cụ mặc định để sử dụng khi bị bỏ qua

GHI CHÚ. Các ứng dụng phụ sẽ kế thừa giá trị của cài đặt này

N/A (không xác định)

app.use(express.static('public'))
3349

Boolean Kích hoạt "X-Powered-By. Express" tiêu đề HTTP

app.use(express.static('public'))
175

Các tùy chọn cho cài đặt `trust proxy`

Đọc Express phía sau proxy để biết thêm thông tin

LoạiGiá trịBoolean

Nếu

app.use(express.static('public'))
175, địa chỉ IP của khách hàng được hiểu là mục ngoài cùng bên trái trong tiêu đề
app.use(express.static('public'))
3341

Nếu

app.use('/static', express.static('public'))
741, ứng dụng được hiểu là đối mặt trực tiếp với Internet và địa chỉ IP của khách hàng được lấy từ
app.use(express.static('public'))
3354. Đây là thiết lập mặc định

Chuỗi
Chuỗi chứa các giá trị được phân tách bằng dấu phẩy
Mảng chuỗi

Địa chỉ IP, mạng con hoặc một mảng địa chỉ IP và mạng con đáng tin cậy. Tên mạng con được cấu hình sẵn là

  • vòng lặp ngược -
    app.use(express.static('public'))
    
    3355,
    app.use(express.static('public'))
    
    3356
  • liên kết địa phương -
    app.use(express.static('public'))
    
    3357,
    app.use(express.static('public'))
    
    3358
  • địa phương duy nhất -
    app.use(express.static('public'))
    
    3359,
    app.use(express.static('public'))
    
    3360,
    app.use(express.static('public'))
    
    3361,
    app.use(express.static('public'))
    
    3362

Đặt địa chỉ IP theo bất kỳ cách nào sau đây

Chỉ định một mạng con duy nhất

app.use(express.static('public'))
302

Chỉ định một mạng con và một địa chỉ

app.use(express.static('public'))
303

Specify multiple subnets as CSV

app.use(express.static('public'))
304

Chỉ định nhiều mạng con dưới dạng một mảng

app.use(express.static('public'))
305

Khi được chỉ định, các địa chỉ IP hoặc mạng con sẽ bị loại trừ khỏi quá trình xác định địa chỉ và địa chỉ IP không đáng tin cậy gần máy chủ ứng dụng nhất được xác định là địa chỉ IP của máy khách

Số

Tin tưởng bước nhảy thứ n từ máy chủ proxy mặt trước với tư cách là máy khách

Function

Triển khai ủy thác tùy chỉnh. Chỉ sử dụng cái này nếu bạn biết bạn đang làm gì

app.use(express.static('public'))
306
Các tùy chọn cho cài đặt `etag`

GHI CHÚ. Các cài đặt này chỉ áp dụng cho tệp động, không phải tệp tĩnh. The express. phần mềm trung gian tĩnh bỏ qua các cài đặt này

Chức năng ETag được triển khai bằng gói etag. For more information, see its documentation

LoạiGiá trịBoolean

app.use(express.static('public'))
175 kích hoạt ETag yếu. This is the default setting.
______6741 vô hiệu hóa hoàn toàn ETag.

StringIf "mạnh", cho phép ETag mạnh.
Nếu "yếu", hãy bật ETag yếu. Chức năng

Custom ETag function implementation. Chỉ sử dụng cái này nếu bạn biết bạn đang làm gì

app.use(express.static('public'))
307

app. sử dụng ([đường dẫn,] gọi lại [, gọi lại. ])

Gắn kết chức năng phần mềm trung gian được chỉ định hoặc các chức năng tại đường dẫn đã chỉ định. the middleware function is executed when the base of the requested path matches

app.use('/static', express.static('public'))
787

Arguments

ArgumentDescriptionDefault____6787Đường dẫn mà chức năng phần mềm trung gian được gọi;
  • A string representing a path
  • Một mô hình đường dẫn
  • A regular expression pattern to match paths
  • An array of combinations of any of the above
For examples, see Path examples. '/' (root path)
app.use(express.static('public'))
4784Callback functions; can be
  • A middleware function
  • A series of middleware functions (separated by commas)
  • An array of middleware functions
  • Một sự kết hợp của tất cả những điều trên

Bạn có thể cung cấp nhiều chức năng gọi lại hoạt động giống như phần mềm trung gian, ngoại trừ việc các lệnh gọi lại này có thể gọi

app.use(express.static('public'))
4785 để bỏ qua (các) lệnh gọi lại tuyến đường còn lại. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there is no reason to proceed with the current route

Since router and app implement the middleware interface, you can use them as you would any other middleware function

For examples, see Middleware callback function examples

None

Sự miêu tả

Một tuyến đường sẽ khớp với bất kỳ con đường nào đi theo con đường của nó ngay lập tức bằng “

app.use(express.static('public'))
3369”. Ví dụ.
app.use(express.static('public'))
3370 sẽ khớp với “/apple”, “/apple/images”, “/apple/images/news”, v.v.

app.use('/static', express.static('public'))
787 mặc định là “/”, phần mềm trung gian được gắn mà không có đường dẫn sẽ được thực thi cho mọi yêu cầu đối với ứng dụng.
Ví dụ: chức năng phần mềm trung gian này sẽ được thực thi cho mọi yêu cầu đối với ứng dụng.

app.use(express.static('public'))
308

GHI CHÚ

Sub-apps will

  • Not inherit the value of settings that have a default value. You must set the value in the sub-app
  • Kế thừa giá trị của cài đặt không có giá trị mặc định

For details, see Application settings

Các chức năng của phần mềm trung gian được thực thi tuần tự, do đó thứ tự đưa vào phần mềm trung gian là rất quan trọng

app.use(express.static('public'))
309

Phần mềm trung gian xử lý lỗi

Phần mềm trung gian xử lý lỗi luôn có bốn đối số. Bạn phải cung cấp bốn đối số để xác định đó là chức năng phần mềm trung gian xử lý lỗi. Ngay cả khi bạn không cần sử dụng đối tượng

app.use(express.static('public'))
3072, bạn phải chỉ định nó để duy trì chữ ký. Nếu không, đối tượng
app.use(express.static('public'))
3072 sẽ được hiểu là phần mềm trung gian thông thường và sẽ không thể xử lý lỗi. Để biết chi tiết về phần mềm trung gian xử lý lỗi, xem. xử lý lỗi

Xác định các hàm phần mềm trung gian xử lý lỗi giống như các hàm phần mềm trung gian khác, ngoại trừ bốn đối số thay vì ba, cụ thể là bằng chữ ký

app.use(express.static('public'))
3374)

app.use(express.static('public'))
330

Ví dụ về đường dẫn

Bảng sau đây cung cấp một số ví dụ đơn giản về các giá trị

app.use('/static', express.static('public'))
787 hợp lệ để gắn phần mềm trung gian

TypeExamplePath

Điều này sẽ khớp với các đường dẫn bắt đầu bằng

app.use(express.static('public'))
3376

app.use(express.static('public'))
331Mô hình đường dẫn

Điều này sẽ khớp với các đường dẫn bắt đầu bằng

app.use(express.static('public'))
3376 và
app.use(express.static('public'))
3378

app.use(express.static('public'))
332

Điều này sẽ khớp với các đường dẫn bắt đầu bằng

app.use(express.static('public'))
3376,
app.use(express.static('public'))
3380,
app.use(express.static('public'))
3381, v.v.

app.use(express.static('public'))
333

Điều này sẽ khớp với các đường dẫn bắt đầu bằng

app.use(express.static('public'))
3376,
app.use(express.static('public'))
3383,
app.use(express.static('public'))
3384,
app.use(express.static('public'))
3385, v.v.

app.use(express.static('public'))
334

Điều này sẽ khớp với các đường dẫn bắt đầu bằng

app.use(express.static('public'))
3386 và
app.use(express.static('public'))
3376

app.use(express.static('public'))
335Biểu thức chính quy

Điều này sẽ khớp với các đường dẫn bắt đầu bằng

app.use(express.static('public'))
3388 và
app.use(express.static('public'))
3389

app.use(express.static('public'))
336Mảng

Điều này sẽ khớp với các đường dẫn bắt đầu bằng

app.use(express.static('public'))
3376,
app.use(express.static('public'))
3391,
app.use(express.static('public'))
3392 và
app.use(express.static('public'))
3393

app.use(express.static('public'))
337

Ví dụ về chức năng gọi lại phần mềm trung gian

Bảng sau đây cung cấp một số ví dụ đơn giản về các hàm phần mềm trung gian có thể được sử dụng làm đối số

app.use(express.static('public'))
4784 cho
app.use(express.static('public'))
3395,
app.use(express.static('public'))
3396 và
app.use(express.static('public'))
4787. Mặc dù các ví dụ dành cho
app.use(express.static('public'))
3395 nhưng chúng cũng hợp lệ cho
app.use(express.static('public'))
3395,
app.use(express.static('public'))
3396 và
app.use(express.static('public'))
4787

Cách sử dụngVí dụPhần mềm trung gian đơn

Bạn có thể xác định và gắn cục bộ chức năng phần mềm trung gian

app.use(express.static('public'))
338

A router is valid middleware

app.use(express.static('public'))
339

Ứng dụng Express là phần mềm trung gian hợp lệ

app.use(express.static('public'))
360Series of Middleware

You can specify more than one middleware function at the same mount path

app.use(express.static('public'))
361Array

Sử dụng một mảng để nhóm phần mềm trung gian một cách hợp lý

app.use(express.static('public'))
362Kết hợp

Bạn có thể kết hợp tất cả các cách cài đặt phần mềm trung gian ở trên

app.use(express.static('public'))
363

Following are some examples of using the express. phần mềm trung gian tĩnh trong ứng dụng Express

Cung cấp nội dung tĩnh cho ứng dụng từ thư mục “công khai” trong thư mục ứng dụng

app.use(express.static('public'))
364

Gắn phần mềm trung gian tại “/static” để chỉ phục vụ nội dung tĩnh khi đường dẫn yêu cầu của chúng có tiền tố là “/static”

app.use(express.static('public'))
365

Tắt ghi nhật ký cho các yêu cầu nội dung tĩnh bằng cách tải phần mềm trung gian ghi nhật ký sau phần mềm trung gian tĩnh

app.use(express.static('public'))
366

Serve static files from multiple directories, but give precedence to “. /public” so với những cái khác

app.use(express.static('public'))
367

Request

The

app.use(express.static('public'))
4779 object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on. In this documentation and by convention, the object is always referred to as
app.use(express.static('public'))
4779 (and the HTTP response is
app.use('/static', express.static('public'))
786) but its actual name is determined by the parameters to the callback function in which you’re working

For example

app.use(express.static('public'))
368

But you could just as well have

app.use(express.static('public'))
369

The

app.use(express.static('public'))
4779 object is an enhanced version of Node’s own request object and supports all built-in fields and methods

Của cải

req. ứng dụng

This property holds a reference to the instance of the Express application that is using the middleware

If you follow the pattern in which you create a module that just exports a middleware function and

app.use(express.static('public'))
3013 it in your main file, then the middleware can access the Express instance via
app.use(express.static('public'))
3607

For example

app.use(express.static('public'))
390
app.use(express.static('public'))
391

req. baseUrl

The URL path on which a router instance was mounted

Thuộc tính

app.use(express.static('public'))
4780 tương tự như thuộc tính mountpath của đối tượng
app.use(express.static('public'))
4771, ngoại trừ
app.use(express.static('public'))
4777 trả về (các) mẫu đường dẫn phù hợp

For example

app.use(express.static('public'))
392

Ngay cả khi bạn sử dụng một mẫu đường dẫn hoặc một tập hợp các mẫu đường dẫn để tải bộ định tuyến, thuộc tính

app.use(express.static('public'))
3611 sẽ trả về chuỗi phù hợp, không phải (các) mẫu. In the following example, the
app.use(express.static('public'))
3612 router is loaded on two path patterns

app.use(express.static('public'))
393

When a request is made to

app.use(express.static('public'))
3613,
app.use(express.static('public'))
4780 is “/greet”. When a request is made to
app.use(express.static('public'))
3615,
app.use(express.static('public'))
4780 is “/hello”

req. cơ thể người

Contains key-value pairs of data submitted in the request body. By default, it is

app.use(express.static('public'))
199, and is populated when you use body-parsing middleware such as
app.use(express.static('public'))
3618 or
app.use(express.static('public'))
3619

Vì hình dạng của

app.use(express.static('public'))
166 dựa trên đầu vào do người dùng kiểm soát, nên tất cả các thuộc tính và giá trị trong đối tượng này đều không đáng tin cậy và phải được xác thực trước khi tin cậy. Ví dụ:
app.use(express.static('public'))
170 có thể bị lỗi theo nhiều cách, chẳng hạn như
app.use(express.static('public'))
171 có thể không có hoặc có thể không phải là một chuỗi và
app.use(express.static('public'))
172 có thể không phải là một hàm mà thay vào đó là một chuỗi hoặc đầu vào khác của người dùng

The following example shows how to use body-parsing middleware to populate

app.use(express.static('public'))
166

app.use(express.static('public'))
394

req. cookies

When using cookie-parser middleware, this property is an object that contains cookies sent by the request. If the request contains no cookies, it defaults to

app.use(express.static('public'))
167

app.use(express.static('public'))
395

If the cookie has been signed, you have to use req. signedCookies

For more information, issues, or concerns, see cookie-parser

req. fresh

When the response is still “fresh” in the client’s cache

app.use(express.static('public'))
175 is returned, otherwise
app.use('/static', express.static('public'))
741 is returned to indicate that the client cache is now stale and the full response should be sent

When a client sends the

app.use(express.static('public'))
3628 request header to indicate an end-to-end reload request, this module will return
app.use('/static', express.static('public'))
741 to make handling these requests transparent

Further details for how cache validation works can be found in the HTTP/1. 1 Đặc tả bộ nhớ đệm

app.use(express.static('public'))
396

req. hostname

Chứa tên máy chủ bắt nguồn từ tiêu đề HTTP

app.use(express.static('public'))
3630

When the

app.use(express.static('public'))
3315 setting does not evaluate to
app.use('/static', express.static('public'))
741, this property will instead get the value from the
app.use(express.static('public'))
3633 header field. This header can be set by the client or by the proxy

If there is more than one

app.use(express.static('public'))
3633 header in the request, the value of the first header is used. This includes a single header with comma-separated values, in which the first value is used

Prior to Express v4. 17. 0, the

app.use(express.static('public'))
3633 could not contain multiple values or be present more than once

app.use(express.static('public'))
397

yêu cầu. ip

Chứa địa chỉ IP từ xa của yêu cầu

When the

app.use(express.static('public'))
3315 setting does not evaluate to
app.use('/static', express.static('public'))
741, the value of this property is derived from the left-most entry in the
app.use(express.static('public'))
3638 header. This header can be set by the client or by the proxy

app.use(express.static('public'))
398

yêu cầu. ips

When the

app.use(express.static('public'))
3315 setting does not evaluate to
app.use('/static', express.static('public'))
741, this property contains an array of IP addresses specified in the
app.use(express.static('public'))
3638 request header. Otherwise, it contains an empty array. This header can be set by the client or by the proxy

Ví dụ: nếu _____ 23638 là ________ 23643, thì ________ 23644 sẽ là ________ 23645, trong đó

app.use(express.static('public'))
3646 là dòng xa nhất về phía hạ lưu

yêu cầu. method

Chứa một chuỗi tương ứng với phương thức HTTP của yêu cầu.

app.use(express.static('public'))
3064,
app.use(express.static('public'))
3648,
app.use(express.static('public'))
3649, and so on

req. url gốc

app.use('/static', express.static('public'))
748 is not a native Express property, it is inherited from Node’s http module

This property is much like

app.use('/static', express.static('public'))
748; however, it retains the original request URL, allowing you to rewrite
app.use('/static', express.static('public'))
748 freely for internal routing purposes. For example, the “mounting” feature of app. use() sẽ viết lại
app.use('/static', express.static('public'))
748 để loại bỏ điểm gắn kết

app.use(express.static('public'))
399

app.use(express.static('public'))
3654 is available both in middleware and router objects, and is a combination of
app.use(express.static('public'))
4780 and
app.use('/static', express.static('public'))
748. Xem xét ví dụ sau

app.use(express.static('public'))
430

req. thông số

This property is an object containing properties mapped to the named route “parameters”. For example, if you have the route

app.use(express.static('public'))
3657, then the “name” property is available as
app.use(express.static('public'))
3658. Đối tượng này mặc định là
app.use(express.static('public'))
167

app.use(express.static('public'))
431

Khi bạn sử dụng biểu thức chính quy cho định nghĩa tuyến đường, các nhóm chụp được cung cấp trong mảng bằng cách sử dụng

app.use(express.static('public'))
3660, trong đó
app.use(express.static('public'))
3661 là nhóm chụp thứ n. This rule is applied to unnamed wild card matches with string routes such as
app.use(express.static('public'))
3662

app.use(express.static('public'))
432

If you need to make changes to a key in

app.use('/static', express.static('public'))
740, use the app. param handler. Changes are applicable only to parameters already defined in the route path

Any changes made to the

app.use('/static', express.static('public'))
740 object in a middleware or route handler will be reset

NOTE. Express tự động giải mã các giá trị trong

app.use('/static', express.static('public'))
740 (sử dụng
app.use(express.static('public'))
3666)

req. path

Contains the path part of the request URL

app.use(express.static('public'))
433

Khi được gọi từ phần mềm trung gian, điểm gắn kết không được bao gồm trong

app.use(express.static('public'))
3667. See app. use() for more details

req. giao thức

Contains the request protocol string. either

app.use(express.static('public'))
3668 or (for TLS requests)
app.use(express.static('public'))
3669

When the

app.use(express.static('public'))
3315 setting does not evaluate to
app.use('/static', express.static('public'))
741, this property will use the value of the
app.use(express.static('public'))
3672 header field if present. This header can be set by the client or by the proxy

app.use(express.static('public'))
434

yêu cầu. query

This property is an object containing a property for each query string parameter in the route. When query parser is set to disabled, it is an empty object

app.use(express.static('public'))
167, otherwise it is the result of the configured query parser

As

app.use(express.static('public'))
3674’s shape is based on user-controlled input, all properties and values in this object are untrusted and should be validated before trusting. For example,
app.use(express.static('public'))
3675 may fail in multiple ways, for example
app.use(express.static('public'))
171 may not be there or may not be a string, and
app.use(express.static('public'))
172 may not be a function and instead a string or other user-input

The value of this property can be configured with the query parser application setting to work how your application needs it. A very popular query string parser is the

app.use(express.static('public'))
4748 module, and this is used by default. The
app.use(express.static('public'))
4748 module is very configurable with many settings, and it may be desirable to use different settings than the default to populate
app.use(express.static('public'))
3674

app.use(express.static('public'))
435

Check out the query parser application setting documentation for other customization options

req. res

This property holds a reference to the response object that relates to this request object

yêu cầu. route

Contains the currently-matched route, a string. For example

app.use(express.static('public'))
436

Example output from the previous snippet

app.use(express.static('public'))
437

yêu cầu. chắc chắn

A Boolean property that is true if a TLS connection is established. Equivalent to

app.use(express.static('public'))
438

req. signedCookies

When using cookie-parser middleware, this property contains signed cookies sent by the request, unsigned and ready for use. Signed cookies reside in a different object to show developer intent; otherwise, a malicious attack could be placed on

app.use(express.static('public'))
3681 values (which are easy to spoof). Note that signing a cookie does not make it “hidden” or encrypted; but simply prevents tampering (because the secret used to sign is private)

If no signed cookies are sent, the property defaults to

app.use(express.static('public'))
167

app.use(express.static('public'))
439

For more information, issues, or concerns, see cookie-parser

req. stale

Indicates whether the request is “stale,” and is the opposite of

app.use(express.static('public'))
3683. For more information, see req. tươi

app.use(express.static('public'))
450

req. subdomains

An array of subdomains in the domain name of the request

app.use(express.static('public'))
451

The application property

app.use(express.static('public'))
3339, which defaults to 2, is used for determining the beginning of the subdomain segments. Để thay đổi hành vi này, hãy thay đổi giá trị của nó bằng ứng dụng. set

req. xhr

A Boolean property that is

app.use(express.static('public'))
175 if the request’s
app.use(express.static('public'))
3686 header field is “XMLHttpRequest”, indicating that the request was issued by a client library such as jQuery

app.use(express.static('public'))
452

phương pháp

req. accepts(types)

Checks if the specified content types are acceptable, based on the request’s

app.use(express.static('public'))
3687 HTTP header field. The method returns the best match, or if none of the specified content types is acceptable, returns
app.use('/static', express.static('public'))
741 (in which case, the application should respond with
app.use(express.static('public'))
3689)

The

app.use(express.static('public'))
161 value may be a single MIME type string (such as “application/json”), an extension name such as “json”, a comma-delimited list, or an array. For a list or array, the method returns the best match (if any)

app.use(express.static('public'))
453

Để biết thêm thông tin hoặc nếu bạn có vấn đề hoặc lo ngại, hãy xem chấp nhận

req. acceptsCharsets(charset [, . ])

Returns the first accepted charset of the specified character sets, based on the request’s

app.use(express.static('public'))
3691 HTTP header field. Nếu không có bộ ký tự nào được chỉ định được chấp nhận, trả về
app.use('/static', express.static('public'))
741

Để biết thêm thông tin hoặc nếu bạn có vấn đề hoặc lo ngại, hãy xem chấp nhận

req. acceptEncodings(encoding [,. ])

Returns the first accepted encoding of the specified encodings, based on the request’s

app.use(express.static('public'))
3693 HTTP header field. If none of the specified encodings is accepted, returns
app.use('/static', express.static('public'))
741

Để biết thêm thông tin hoặc nếu bạn có vấn đề hoặc lo ngại, hãy xem chấp nhận

yêu cầu. acceptsLanguages(lang [, . ])

Trả về ngôn ngữ được chấp nhận đầu tiên trong số các ngôn ngữ đã chỉ định, dựa trên trường tiêu đề HTTP

app.use(express.static('public'))
3695 của yêu cầu. Nếu không có ngôn ngữ cụ thể nào được chấp nhận, trả về
app.use('/static', express.static('public'))
741

Để biết thêm thông tin hoặc nếu bạn có vấn đề hoặc lo ngại, hãy xem chấp nhận

req. get(field)

Returns the specified HTTP request header field (case-insensitive match). The

app.use(express.static('public'))
3697 and
app.use(express.static('public'))
3698 fields are interchangeable

app.use(express.static('public'))
454

Aliased as

app.use(express.static('public'))
3699

req. là (loại)

Returns the matching content type if the incoming request’s “Content-Type” HTTP header field matches the MIME type specified by the

app.use(express.static('public'))
161 parameter. If the request has no body, returns
app.use(express.static('public'))
181. Returns
app.use('/static', express.static('public'))
741 otherwise

app.use(express.static('public'))
455

For more information, or if you have issues or concerns, see type-is

yêu cầu. tham số (tên [, giá trị mặc định])

Deprecated. Use either

app.use('/static', express.static('public'))
740,
app.use(express.static('public'))
166 or
app.use(express.static('public'))
3674, as applicable

Trả về giá trị của tham số

app.use(express.static('public'))
4793 khi có mặt

app.use(express.static('public'))
456

Tra cứu được thực hiện theo thứ tự sau

  • app.use('/static', express.static('public'))
    
    740
  • app.use(express.static('public'))
    
    166
  • app.use(express.static('public'))
    
    3674

Optionally, you can specify

app.use(express.static('public'))
3910 to set a default value if the parameter is not found in any of the request objects

Direct access to

app.use(express.static('public'))
166,
app.use('/static', express.static('public'))
740, and
app.use(express.static('public'))
3674 should be favoured for clarity - unless you truly accept input from each object

Body-parsing middleware must be loaded for

app.use(express.static('public'))
3914 to work predictably. Refer req. body for details

req. range(size[, options])

app.use(express.static('public'))
3915 header parser

The

app.use(express.static('public'))
3916 parameter is the maximum size of the resource

The

app.use(express.static('public'))
173 parameter is an object that can have the following properties

Thuộc tínhTypeDescription
app.use(express.static('public'))
3918BooleanChỉ định xem có nên kết hợp các phạm vi chồng chéo và liền kề hay không, mặc định là
app.use('/static', express.static('public'))
741. Khi
app.use(express.static('public'))
175, phạm vi sẽ được kết hợp và trả về như thể chúng được chỉ định theo cách đó trong tiêu đề

An array of ranges will be returned or negative numbers indicating an error parsing

  • app.use(express.static('public'))
    
    3921 signals a malformed header string
  • app.use(express.static('public'))
    
    3922 signals an unsatisfiable range
app.use(express.static('public'))
457

Response

The

app.use('/static', express.static('public'))
786 object represents the HTTP response that an Express app sends when it gets an HTTP request

In this documentation and by convention, the object is always referred to as

app.use('/static', express.static('public'))
786 (and the HTTP request is
app.use(express.static('public'))
4779) but its actual name is determined by the parameters to the callback function in which you’re working

For example

app.use(express.static('public'))
368

But you could just as well have

app.use(express.static('public'))
369

The

app.use('/static', express.static('public'))
786 object is an enhanced version of Node’s own response object and supports all built-in fields and methods

Của cải

res. app

This property holds a reference to the instance of the Express application that is using the middleware

app.use(express.static('public'))
3927 is identical to the req. app property in the request object

Boolean property that indicates if the app sent HTTP headers for the response

app.use(express.static('public'))
100

res. locals

Use this property to set variables accessible in templates rendered with res. render. The variables set on

app.use(express.static('public'))
3928 are available within a single request-response cycle, and will not be shared between requests

In order to keep local variables for use in template rendering between requests, use app. locals instead

Thuộc tính này hữu ích để hiển thị thông tin cấp yêu cầu, chẳng hạn như tên đường dẫn yêu cầu, người dùng được xác thực, cài đặt người dùng, v.v. cho các mẫu được hiển thị trong ứng dụng

app.use(express.static('public'))
101

phương pháp

res. nối thêm (trường [, giá trị])

app.use(express.static('public'))
3929 is supported by Express v4. 11. 0+

Nối thêm

app.use(express.static('public'))
3309 đã chỉ định vào tiêu đề phản hồi HTTP
app.use(express.static('public'))
3931. Nếu tiêu đề chưa được đặt, nó sẽ tạo tiêu đề với giá trị đã chỉ định. Tham số
app.use(express.static('public'))
3309 có thể là một chuỗi hoặc một mảng

Ghi chú. gọi

app.use(express.static('public'))
3933 sau
app.use(express.static('public'))
3929 sẽ đặt lại giá trị tiêu đề đã đặt trước đó

app.use(express.static('public'))
102

độ phân giải. tệp đính kèm ([tên tệp])

Đặt trường tiêu đề phản hồi HTTP

app.use(express.static('public'))
3935 thành “tệp đính kèm”. Nếu một
app.use(express.static('public'))
3936 được cung cấp, thì nó sẽ đặt Loại nội dung dựa trên tên tiện ích mở rộng thông qua
app.use(express.static('public'))
3937 và đặt tham số “filename=” của
app.use(express.static('public'))
3935

app.use(express.static('public'))
103

Đặt cookie

app.use(express.static('public'))
4793 thành
app.use(express.static('public'))
3309. The
app.use(express.static('public'))
3309 parameter may be a string or object converted to JSON

The

app.use(express.static('public'))
173 parameter is an object that can have the following properties

Thuộc tínhTypeDescription
app.use(express.static('public'))
3943StringTên miền cho cookie. Mặc định là tên miền của ứng dụng.
app.use(express.static('public'))
3944FunctionA synchronous function used for cookie value encoding. Mặc định là
app.use(express.static('public'))
3945.
app.use(express.static('public'))
3946DateNgày hết hạn của cookie tính bằng GMT. Nếu không được chỉ định hoặc được đặt thành 0, hãy tạo cookie phiên.
app.use(express.static('public'))
3947BooleanFlags the cookie to be accessible only by the web server.
app.use('/static', express.static('public'))
764SốTùy chọn thuận tiện để đặt thời gian hết hạn so với thời gian hiện tại tính bằng mili giây.
app.use('/static', express.static('public'))
787StringPath cho cookie. Defaults to “/”.
app.use(express.static('public'))
3950StringValue of the “Priority” Set-Cookie attribute.
app.use(express.static('public'))
3951BooleanMarks the cookie to be used with HTTPS only.
app.use(express.static('public'))
3952BooleanCho biết liệu cookie có được ký hay không.
app.use(express.static('public'))
3953Boolean or StringValue of the “SameSite” Set-Cookie attribute. More information at https. //công cụ. ietf. org/html/draft-ietf-httpbis-cookie-same-site-00#section-4. 1. 1

Tất cả những gì

app.use(express.static('public'))
3954 làm là đặt tiêu đề HTTP
app.use(express.static('public'))
3955 với các tùy chọn được cung cấp. Bất kỳ tùy chọn nào không được chỉ định mặc định theo giá trị được nêu trong RFC 6265

For example

app.use(express.static('public'))
104

Ví dụ, bạn có thể đặt nhiều cookie trong một phản hồi bằng cách gọi

app.use(express.static('public'))
3956 nhiều lần

app.use(express.static('public'))
105

The

app.use(express.static('public'))
3944 option allows you to choose the function used for cookie value encoding. Không hỗ trợ các chức năng không đồng bộ

Trường hợp sử dụng ví dụ. Bạn cần đặt cookie trên toàn miền cho một trang web khác trong tổ chức của mình. This other site (not under your administrative control) does not use URI-encoded cookie values

app.use(express.static('public'))
106

Tùy chọn

app.use('/static', express.static('public'))
764 là tùy chọn tiện lợi để đặt “hết hạn” so với thời gian hiện tại tính bằng mili giây. Sau đây là tương đương với ví dụ thứ hai ở trên

app.use(express.static('public'))
107

Bạn có thể truyền một đối tượng dưới dạng tham số

app.use(express.static('public'))
3309;

app.use(express.static('public'))
108

Khi sử dụng phần mềm trung gian trình phân tích cú pháp cookie, phương pháp này cũng hỗ trợ cookie đã ký. Chỉ cần bao gồm tùy chọn

app.use(express.static('public'))
3952 được đặt thành
app.use(express.static('public'))
175. Sau đó,
app.use(express.static('public'))
3954 sẽ sử dụng bí mật được truyền cho
app.use(express.static('public'))
3964 để ký giá trị

app.use(express.static('public'))
109

Sau đó, bạn có thể truy cập giá trị này thông qua yêu cầu. signedCookie object

độ phân giải. clearCookie(name [, options])

Xóa cookie được chỉ định bởi

app.use(express.static('public'))
4793. Để biết chi tiết về đối tượng ________ 1173, xem màu đỏ. cookie()

Các trình duyệt web và các ứng dụng khách tuân thủ khác sẽ chỉ xóa cookie nếu

app.use(express.static('public'))
173 đã cho giống hệt với các trình duyệt được cung cấp cho res. cookie(), ngoại trừ
app.use(express.static('public'))
3946 và
app.use('/static', express.static('public'))
764

app.use(express.static('public'))
110

độ phân giải. download(path [, filename] [, options] [, fn])

Chuyển tệp tại

app.use('/static', express.static('public'))
787 dưới dạng “tệp đính kèm”. Typically, browsers will prompt the user for download. By default, the
app.use(express.static('public'))
3935 header “filename=” parameter is derrived from the
app.use('/static', express.static('public'))
787 argument, but can be overridden with the
app.use(express.static('public'))
3936 parameter. If
app.use('/static', express.static('public'))
787 is relative, then it will be based on the current working directory of the process or the
app.use('/static', express.static('public'))
747 option, if provided

This API provides access to data on the running file system. Ensure that either (a) the way in which the

app.use('/static', express.static('public'))
787 argument was constructed is secure if it contains user input or (b) set the
app.use('/static', express.static('public'))
747 option to the absolute path of a directory to contain access within

Khi tùy chọn

app.use('/static', express.static('public'))
747 được cung cấp, Express sẽ xác thực rằng đường dẫn tương đối được cung cấp dưới dạng
app.use('/static', express.static('public'))
787 sẽ giải quyết trong tùy chọn
app.use('/static', express.static('public'))
747 đã cho

Bảng sau đây cung cấp thông tin chi tiết về tham số

app.use(express.static('public'))
173

The optional

app.use(express.static('public'))
173 argument is supported by Express v4. 16. 0 trở đi

PropertyDescriptionDefaultAvailability

app.use('/static', express.static('public'))
764Đặt thuộc tính max-age của tiêu đề
app.use('/static', express.static('public'))
763 tính bằng mili giây hoặc chuỗi ở định dạng ms04. 16+______6747Thư mục gốc cho tên tệp tương đối. 4. 18+
app.use('/static', express.static('public'))
770Sets the
app.use('/static', express.static('public'))
771 header to the last modified date of the file on the OS. Đặt
app.use('/static', express.static('public'))
741 để tắt nó. Enabled4. 16+
app.use(express.static('public'))
3989Object containing HTTP headers to serve with the file. The header
app.use(express.static('public'))
3935 will be overriden by the
app.use(express.static('public'))
3936 argument. 4. 16+______6752Tùy chọn phân phối tệp dotfile. Các giá trị có thể là “cho phép”, “từ chối”, “bỏ qua”. “phớt lờ”4. 16+
app.use(express.static('public'))
3993Enable or disable accepting ranged requests.
app.use(express.static('public'))
1754. 16+______23995Bật hoặc tắt cài đặt tiêu đề phản hồi
app.use('/static', express.static('public'))
763.
app.use(express.static('public'))
1754. 16+______6761Bật hoặc tắt chỉ thị
app.use('/static', express.static('public'))
761 trong tiêu đề phản hồi
app.use('/static', express.static('public'))
763. Nếu được bật, tùy chọn
app.use('/static', express.static('public'))
764 cũng phải được chỉ định để bật bộ nhớ đệm. The
app.use('/static', express.static('public'))
761 directive will prevent supported clients from making conditional requests during the life of the
app.use('/static', express.static('public'))
764 option to check if the file has changed.
app.use('/static', express.static('public'))
7414. 16+

The method invokes the callback function

app.use(express.static('public'))
4305 when the transfer is complete or when an error occurs. Nếu chức năng gọi lại được chỉ định và xảy ra lỗi, chức năng gọi lại phải xử lý rõ ràng quy trình phản hồi bằng cách kết thúc chu kỳ phản hồi yêu cầu hoặc bằng cách chuyển điều khiển sang tuyến tiếp theo

app.use(express.static('public'))
111

res. end([data] [, encoding])

Kết thúc quá trình phản hồi. Phương pháp này thực sự đến từ lõi Node, cụ thể là phản hồi. phương thức kết thúc () của http. ServerResponse

Use to quickly end the response without any data. If you need to respond with data, instead use methods such as res. gửi() và res. json()

app.use(express.static('public'))
112

độ phân giải. định dạng (đối tượng)

Performs content-negotiation on the

app.use(express.static('public'))
3687 HTTP header on the request object, when present. Nó sử dụng yêu cầu. accepts() to select a handler for the request, based on the acceptable types ordered by their quality values. If the header is not specified, the first callback is invoked. Khi không tìm thấy kết quả phù hợp, máy chủ sẽ phản hồi với 406 “Không được chấp nhận” hoặc gọi lại cuộc gọi lại
app.use(express.static('public'))
4307

Tiêu đề phản hồi

app.use(express.static('public'))
160 được đặt khi chọn gọi lại. However, you may alter this within the callback using methods such as
app.use(express.static('public'))
3933 or
app.use(express.static('public'))
3937

Ví dụ sau đây sẽ phản hồi bằng

app.use(express.static('public'))
4311 khi trường tiêu đề
app.use(express.static('public'))
3687 được đặt thành “application/json” hoặc “*/json” (tuy nhiên nếu là “*/*”, thì phản hồi sẽ là “hey”)

app.use(express.static('public'))
113

In addition to canonicalized MIME types, you may also use extension names mapped to these types for a slightly less verbose implementation

app.use(express.static('public'))
114

độ phân giải. lấy (trường)

Trả về tiêu đề phản hồi HTTP được chỉ định bởi

app.use(express.static('public'))
3931. Trận đấu không phân biệt chữ hoa chữ thường

app.use(express.static('public'))
115

độ phân giải. json([nội dung])

Gửi phản hồi JSON. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON. xâu chuỗi ()

Tham số có thể là bất kỳ loại JSON nào, bao gồm đối tượng, mảng, chuỗi, Boolean, số hoặc null và bạn cũng có thể sử dụng nó để chuyển đổi các giá trị khác thành JSON

app.use(express.static('public'))
116

độ phân giải. jsonp([cơ thể])

Gửi phản hồi JSON có hỗ trợ JSONP. This method is identical to

app.use(express.static('public'))
4314, except that it opts-in to JSONP callback support

app.use(express.static('public'))
117

Theo mặc định, tên gọi lại JSONP chỉ đơn giản là

app.use(express.static('public'))
4784. Ghi đè điều này bằng cài đặt tên gọi lại jsonp

The following are some examples of JSONP responses using the same code

app.use(express.static('public'))
118

Tham gia

app.use(express.static('public'))
4316 được cung cấp làm thuộc tính của tham số để điền vào trường tiêu đề HTTP
app.use(express.static('public'))
4317 của phản hồi

For example, the following call

app.use(express.static('public'))
119

Yields the following results

app.use(express.static('public'))
120

Sets the response

app.use(express.static('public'))
4318 HTTP header to the specified
app.use('/static', express.static('public'))
787 parameter

app.use(express.static('public'))
121

Giá trị

app.use('/static', express.static('public'))
787 của “back” có ý nghĩa đặc biệt, nó đề cập đến URL được chỉ định trong tiêu đề
app.use(express.static('public'))
3698 của yêu cầu. If the
app.use(express.static('public'))
3698 header was not specified, it refers to “/”

After encoding the URL, if not encoded already, Express passes the specified URL to the browser in the

app.use(express.static('public'))
4318 header, without any validation

Các trình duyệt chịu trách nhiệm lấy URL dự định từ URL hiện tại hoặc URL giới thiệu và URL được chỉ định trong tiêu đề

app.use(express.static('public'))
4318;

độ phân giải. chuyển hướng ([trạng thái,] đường dẫn)

Chuyển hướng đến URL bắt nguồn từ

app.use('/static', express.static('public'))
787 được chỉ định, với
app.use(express.static('public'))
4326 được chỉ định, một số nguyên dương tương ứng với mã trạng thái HTTP. Nếu không được chỉ định,
app.use(express.static('public'))
4326 mặc định là “302 “Đã tìm thấy”

app.use(express.static('public'))
122

Redirects can be a fully-qualified URL for redirecting to a different site

app.use(express.static('public'))
123

Redirects can be relative to the root of the host name. For example, if the application is on

app.use(express.static('public'))
4328, the following would redirect to the URL
app.use(express.static('public'))
4329

app.use(express.static('public'))
124

Redirects can be relative to the current URL. For example, from

app.use(express.static('public'))
4330 (notice the trailing slash), the following would redirect to the URL
app.use(express.static('public'))
4331

app.use(express.static('public'))
125

Redirecting to

app.use(express.static('public'))
4332 from
app.use(express.static('public'))
4333 (no trailing slash), will redirect to
app.use(express.static('public'))
4334

If you found the above behavior confusing, think of path segments as directories (with trailing slashes) and files, it will start to make sense

Chuyển hướng tương đối đường dẫn cũng có thể. If you were on

app.use(express.static('public'))
4328, the following would redirect to
app.use(express.static('public'))
4336

app.use(express.static('public'))
126

Chuyển hướng

app.use(express.static('public'))
4337 chuyển hướng yêu cầu trở lại người giới thiệu, mặc định là
app.use(express.static('public'))
3369 khi người giới thiệu bị thiếu

app.use(express.static('public'))
127

độ phân giải. render(view [, locals] [, callback])

Renders a

app.use(express.static('public'))
4339 and sends the rendered HTML string to the client. Optional parameters

  • app.use(express.static('public'))
    
    4340, an object whose properties define local variables for the view
  • app.use(express.static('public'))
    
    4784, chức năng gọi lại. Nếu được cung cấp, phương thức này sẽ trả về cả lỗi có thể xảy ra và chuỗi được hiển thị, nhưng không thực hiện phản hồi tự động. When an error occurs, the method invokes
    app.use('/static', express.static('public'))
    
    783 internally

Đối số

app.use(express.static('public'))
4339 là một chuỗi là đường dẫn tệp của tệp xem để hiển thị. Đây có thể là đường dẫn tuyệt đối hoặc đường dẫn liên quan đến cài đặt
app.use(express.static('public'))
3344. Nếu đường dẫn không chứa phần mở rộng tệp, thì cài đặt
app.use(express.static('public'))
3348 sẽ xác định phần mở rộng tệp. If the path does contain a file extension, then Express will load the module for the specified template engine (via
app.use(express.static('public'))
3013) and render it using the loaded module’s
app.use(express.static('public'))
4347 function

Để biết thêm thông tin, hãy xem Sử dụng công cụ mẫu với Express

GHI CHÚ. The

app.use(express.static('public'))
4339 argument performs file system operations like reading a file from disk and evaluating Node. js và vì lý do bảo mật, không nên chứa đầu vào từ người dùng cuối

The local variable

app.use(express.static('public'))
3305 enables view caching. Đặt nó thành
app.use(express.static('public'))
175, để lưu trữ chế độ xem trong quá trình phát triển;

app.use(express.static('public'))
128

res. req

Thuộc tính này giữ một tham chiếu đến đối tượng yêu cầu liên quan đến đối tượng phản hồi này

res. gửi([cơ thể])

Gửi phản hồi HTTP

The

app.use(express.static('public'))
164 parameter can be a
app.use(express.static('public'))
197 object, a
app.use(express.static('public'))
4353, an object,
app.use(express.static('public'))
4354, or an
app.use(express.static('public'))
4355. Ví dụ

app.use(express.static('public'))
129

This method performs many useful tasks for simple non-streaming responses. Ví dụ: nó tự động chỉ định trường tiêu đề phản hồi HTTP

app.use(express.static('public'))
4356 (trừ khi được xác định trước đó) và cung cấp hỗ trợ làm mới bộ đệm HEAD và HTTP tự động

Khi tham số là một đối tượng

app.use(express.static('public'))
197, phương thức này sẽ đặt trường tiêu đề phản hồi
app.use(express.static('public'))
160 thành “application/octet-stream”, trừ khi được định nghĩa trước đó như minh họa bên dưới

app.use(express.static('public'))
130

Khi tham số là một

app.use(express.static('public'))
4353, phương thức này đặt
app.use(express.static('public'))
160 thành “text/html”

app.use(express.static('public'))
131

When the parameter is an

app.use(express.static('public'))
4355 or
app.use(express.static('public'))
4362, Express responds with the JSON representation

app.use(express.static('public'))
132

res. sendFile(path [, options] [, fn])

app.use(express.static('public'))
4363 is supported by Express v4. 8. 0 trở đi

Chuyển tệp tại

app.use('/static', express.static('public'))
787 đã cho. Đặt trường tiêu đề HTTP phản hồi
app.use(express.static('public'))
160 dựa trên phần mở rộng của tên tệp. Unless the
app.use('/static', express.static('public'))
747 option is set in the options object,
app.use('/static', express.static('public'))
787 must be an absolute path to the file

API này cung cấp quyền truy cập vào dữ liệu trên hệ thống tệp đang chạy. Đảm bảo rằng (a) cách mà đối số

app.use('/static', express.static('public'))
787 được xây dựng thành một đường dẫn tuyệt đối là an toàn nếu nó chứa đầu vào của người dùng hoặc (b) đặt tùy chọn
app.use('/static', express.static('public'))
747 thành đường dẫn tuyệt đối của một thư mục để chứa quyền truy cập bên trong

Khi tùy chọn

app.use('/static', express.static('public'))
747 được cung cấp, đối số
app.use('/static', express.static('public'))
787 được phép là một đường dẫn tương đối, bao gồm chứa
app.use(express.static('public'))
4372. Express sẽ xác thực rằng đường dẫn tương đối được cung cấp dưới dạng
app.use('/static', express.static('public'))
787 sẽ giải quyết trong tùy chọn
app.use('/static', express.static('public'))
747 đã cho

Bảng sau đây cung cấp thông tin chi tiết về tham số

app.use(express.static('public'))
173

PropertyDescriptionDefaultAvailability

app.use('/static', express.static('public'))
764Đặt thuộc tính max-age của tiêu đề
app.use('/static', express.static('public'))
763 tính bằng mili giây hoặc một chuỗi ở định dạng ms0
app.use('/static', express.static('public'))
747Thư mục gốc cho tên tệp tương đối.
app.use('/static', express.static('public'))
770Sets the
app.use('/static', express.static('public'))
771 header to the last modified date of the file on the OS. Set
app.use('/static', express.static('public'))
741 to disable it. Đã bật4. 9. 0+______23989Object chứa các tiêu đề HTTP để phân phát cùng với tệp.
app.use('/static', express.static('public'))
752Tùy chọn phân phối tệp dotfile. Các giá trị có thể là “cho phép”, “từ chối”, “bỏ qua”. “ignore”
app.use(express.static('public'))
3993Enable or disable accepting ranged requests.
app.use(express.static('public'))
1754. 14+
app.use(express.static('public'))
3995Enable or disable setting
app.use('/static', express.static('public'))
763 response header.
app.use(express.static('public'))
1754. 14+______6761Bật hoặc tắt chỉ thị
app.use('/static', express.static('public'))
761 trong tiêu đề phản hồi
app.use('/static', express.static('public'))
763. Nếu được bật, tùy chọn
app.use('/static', express.static('public'))
764 cũng phải được chỉ định để bật bộ nhớ đệm. Chỉ thị
app.use('/static', express.static('public'))
761 sẽ ngăn khách hàng được hỗ trợ thực hiện các yêu cầu có điều kiện trong suốt thời gian tồn tại của tùy chọn
app.use('/static', express.static('public'))
764 để kiểm tra xem tệp đã thay đổi chưa.
app.use('/static', express.static('public'))
7414. 16+

The method invokes the callback function

app.use(express.static('public'))
4305 when the transfer is complete or when an error occurs. Nếu chức năng gọi lại được chỉ định và xảy ra lỗi, chức năng gọi lại phải xử lý rõ ràng quy trình phản hồi bằng cách kết thúc chu kỳ phản hồi yêu cầu hoặc bằng cách chuyển điều khiển sang tuyến tiếp theo

Đây là một ví dụ về việc sử dụng

app.use(express.static('public'))
4397 với tất cả các đối số của nó

app.use(express.static('public'))
133

Ví dụ sau minh họa việc sử dụng

app.use(express.static('public'))
4397 để cung cấp hỗ trợ chi tiết cho việc cung cấp tệp

app.use(express.static('public'))
134

Để biết thêm thông tin hoặc nếu bạn có vấn đề hoặc lo ngại, hãy xem gửi

res. sendStatus(statusCode)

Đặt mã trạng thái HTTP phản hồi thành

app.use(express.static('public'))
4399 và gửi thông báo trạng thái đã đăng ký dưới dạng nội dung phản hồi văn bản. If an unknown status code is specified, the response body will just be the code number

app.use(express.static('public'))
135

Một số phiên bản của Node. js sẽ ném khi

app.use(express.static('public'))
4500 được đặt thành mã trạng thái HTTP không hợp lệ (nằm ngoài phạm vi
app.use(express.static('public'))
4501 đến
app.use(express.static('public'))
4502). Tham khảo tài liệu máy chủ HTTP cho Node. phiên bản js đang được sử dụng

More about HTTP Status Codes

độ phân giải. đặt (trường [, giá trị])

Sets the response’s HTTP header

app.use(express.static('public'))
3931 to
app.use(express.static('public'))
3309. To set multiple fields at once, pass an object as the parameter

app.use(express.static('public'))
136

Bí danh là

app.use(express.static('public'))
4505

res. trạng thái (mã)

Sets the HTTP status for the response. It is a chainable alias of Node’s response. statusCode

app.use(express.static('public'))
137

res. type(type)

Sets the

app.use(express.static('public'))
160 HTTP header to the MIME type as determined by the specified
app.use(express.static('public'))
161. If
app.use(express.static('public'))
161 contains the “/” character, then it sets the
app.use(express.static('public'))
160 to the exact value of
app.use(express.static('public'))
161, otherwise it is assumed to be a file extension and the MIME type is looked up in a mapping using the
app.use(express.static('public'))
4511 method

app.use(express.static('public'))
138

độ phân giải. vary(field)

Thêm trường vào tiêu đề phản hồi

app.use(express.static('public'))
4512, nếu nó chưa có

app.use(express.static('public'))
139

Router

Một đối tượng

app.use('/static', express.static('public'))
746 là một trường hợp riêng biệt của phần mềm trung gian và các tuyến đường. Bạn có thể coi nó như một “ứng dụng nhỏ”, chỉ có khả năng thực hiện các chức năng định tuyến và phần mềm trung gian. Mọi ứng dụng Express đều có bộ định tuyến ứng dụng tích hợp

A router behaves like middleware itself, so you can use it as an argument to app. use() hoặc làm đối số cho phương thức use() của bộ định tuyến khác

The top-level

app.use(express.static('public'))
159 object has a Router() method that creates a new
app.use('/static', express.static('public'))
746 object

Khi bạn đã tạo một đối tượng bộ định tuyến, bạn có thể thêm phần mềm trung gian và các tuyến phương thức HTTP (chẳng hạn như

app.use('/static', express.static('public'))
743,
app.use('/static', express.static('public'))
744,
app.use('/static', express.static('public'))
745, v.v.) vào nó giống như một ứng dụng. For example

app.use(express.static('public'))
140

Sau đó, bạn có thể sử dụng bộ định tuyến cho một URL gốc cụ thể theo cách này, tách các tuyến đường của bạn thành các tệp hoặc thậm chí là các ứng dụng nhỏ

app.use(express.static('public'))
141

phương pháp

router. all(path, [callback, . ] gọi lại)

Phương thức này giống như các phương thức của

app.use(express.static('public'))
4519, ngoại trừ việc nó phù hợp với tất cả các phương thức HTTP (động từ)

Phương pháp này cực kỳ hữu ích để ánh xạ logic “toàn cầu” cho các tiền tố đường dẫn cụ thể hoặc các đối sánh tùy ý. Ví dụ: nếu bạn đặt tuyến đường sau ở trên cùng của tất cả các định nghĩa tuyến đường khác, nó sẽ yêu cầu tất cả các tuyến đường từ thời điểm đó trở đi sẽ yêu cầu xác thực và tự động tải người dùng. Hãy nhớ rằng những cuộc gọi lại này không phải đóng vai trò là điểm cuối;

app.use(express.static('public'))
142

Or the equivalent

app.use(express.static('public'))
143

Một ví dụ khác về điều này là chức năng “toàn cầu” được liệt kê trắng. Đây là ví dụ giống như trước, nhưng nó chỉ giới hạn các đường dẫn có tiền tố là “/api”

app.use(express.static('public'))
144

router. PHƯƠNG PHÁP (đường dẫn, [gọi lại,. ] callback)

Các phương thức

app.use(express.static('public'))
4519 cung cấp chức năng định tuyến trong Express, trong đó METHOD là một trong các phương thức HTTP, chẳng hạn như GET, PUT, POST, v.v., viết thường. Do đó, các phương pháp thực tế là
app.use(express.static('public'))
4523,
app.use(express.static('public'))
4524,
app.use(express.static('public'))
4525, v.v.

The

app.use(express.static('public'))
4523 function is automatically called for the HTTP
app.use(express.static('public'))
3063 method in addition to the
app.use(express.static('public'))
3064 method if
app.use(express.static('public'))
4529 was not called for the path before
app.use(express.static('public'))
4523

You can provide multiple callbacks, and all are treated equally, and behave just like middleware, except that these callbacks may invoke

app.use(express.static('public'))
4785 to bypass the remaining route callback(s). Bạn có thể sử dụng cơ chế này để thực hiện các điều kiện trước trên một tuyến đường, sau đó chuyển quyền kiểm soát cho các tuyến tiếp theo khi không có lý do gì để tiếp tục với tuyến đã khớp

Đoạn mã sau minh họa định nghĩa tuyến đường đơn giản nhất có thể. Express translates the path strings to regular expressions, used internally to match incoming requests. Các chuỗi truy vấn không được xem xét khi thực hiện các kết quả khớp này, ví dụ: “GET /” sẽ khớp với tuyến sau, cũng như “GET /?name=tobi”

app.use(express.static('public'))
145

Bạn cũng có thể sử dụng các biểu thức chính quy—hữu ích nếu bạn có các ràng buộc rất cụ thể, ví dụ: các ràng buộc sau sẽ khớp với “GET /commits/71dbb9c” cũng như “GET /commits/71dbb9c”. 4c084f9”

app.use(express.static('public'))
146

router. param(tên, gọi lại)

Adds callback triggers to route parameters, where

app.use(express.static('public'))
4793 is the name of the parameter and
app.use(express.static('public'))
4784 is the callback function. Although
app.use(express.static('public'))
4793 is technically optional, using this method without it is deprecated starting with Express v4. 11. 0 (xem bên dưới)

Các tham số của hàm gọi lại là

  • app.use(express.static('public'))
    
    4779, the request object
  • app.use('/static', express.static('public'))
    
    786, đối tượng phản hồi
  • app.use(express.static('public'))
    
    3072, indicating the next middleware function
  • Giá trị của tham số
    app.use(express.static('public'))
    
    4793
  • The name of the parameter

Unlike

app.use(express.static('public'))
3083,
app.use(express.static('public'))
4540 does not accept an array of route parameters

For example, when

app.use(express.static('public'))
3075 is present in a route path, you may map user loading logic to automatically provide
app.use(express.static('public'))
3076 to the route, or perform validations on the parameter input

app.use(express.static('public'))
147

Param callback functions are local to the router on which they are defined. Chúng không được kế thừa bởi các ứng dụng hoặc bộ định tuyến được gắn kết. Do đó, các cuộc gọi lại param được xác định trên

app.use('/static', express.static('public'))
746 sẽ chỉ được kích hoạt bởi các tham số tuyến đường được xác định trên các tuyến đường
app.use('/static', express.static('public'))
746

A param callback will be called only once in a request-response cycle, even if the parameter is matched in multiple routes, as shown in the following examples

app.use(express.static('public'))
148

Trên

app.use(express.static('public'))
3079, phần sau được in

app.use(express.static('public'))
470

Phần sau đây mô tả

app.use(express.static('public'))
4546, không được dùng nữa kể từ v4. 11. 0

Hành vi của phương thức

app.use(express.static('public'))
4547 có thể được thay đổi hoàn toàn bằng cách chỉ chuyển một hàm cho
app.use(express.static('public'))
4540. Hàm này là một triển khai tùy chỉnh về cách hoạt động của
app.use(express.static('public'))
4547 - hàm này chấp nhận hai tham số và phải trả về một phần mềm trung gian

The first parameter of this function is the name of the URL parameter that should be captured, the second parameter can be any JavaScript object which might be used for returning the middleware implementation

Phần mềm trung gian được trả về bởi hàm quyết định hành vi của những gì sẽ xảy ra khi tham số URL bị bắt

Trong ví dụ này, chữ ký

app.use(express.static('public'))
4547 được sửa đổi thành
app.use(express.static('public'))
4551. Instead of accepting a name and a callback,
app.use(express.static('public'))
4540 will now accept a name and a number

app.use(express.static('public'))
150

Trong ví dụ này, chữ ký

app.use(express.static('public'))
4547 vẫn giữ nguyên, nhưng thay vì gọi lại phần mềm trung gian, chức năng kiểm tra loại dữ liệu tùy chỉnh đã được xác định để xác thực loại dữ liệu của id người dùng

app.use(express.static('public'))
151

router. route(path)

Trả về một thể hiện của một tuyến mà sau đó bạn có thể sử dụng để xử lý các động từ HTTP với phần mềm trung gian tùy chọn. Use

app.use(express.static('public'))
4554 to avoid duplicate route naming and thus typing errors

Dựa trên ví dụ về

app.use(express.static('public'))
4540 ở trên, đoạn mã sau cho biết cách sử dụng
app.use(express.static('public'))
4554 để chỉ định các trình xử lý phương thức HTTP khác nhau

app.use(express.static('public'))
152

This approach re-uses the single

app.use(express.static('public'))
4557 path and adds handlers for various HTTP methods

NOTE. When you use

app.use(express.static('public'))
4554, middleware ordering is based on when the route is created, not when method handlers are added to the route. Với mục đích này, bạn có thể coi các trình xử lý phương thức thuộc về tuyến đường mà chúng được thêm vào

router. sử dụng ([đường dẫn], [chức năng,. ] hàm số)

Uses the specified middleware function or functions, with optional mount path

app.use('/static', express.static('public'))
787, that defaults to “/”

This method is similar to app. use(). Một ví dụ đơn giản và trường hợp sử dụng được mô tả bên dưới. See app. use() for more information

Middleware is like a plumbing pipe. các yêu cầu bắt đầu tại chức năng phần mềm trung gian đầu tiên được xác định và hoạt động theo cách của chúng "xuống" quá trình xử lý ngăn xếp phần mềm trung gian cho từng đường dẫn mà chúng khớp

app.use(express.static('public'))
153

Đường dẫn "gắn kết" bị tước và không hiển thị đối với chức năng phần mềm trung gian. Tác dụng chính của tính năng này là chức năng phần mềm trung gian được gắn có thể hoạt động mà không cần thay đổi mã bất kể tên đường dẫn "tiền tố" của nó

Thứ tự mà bạn xác định phần mềm trung gian với

app.use(express.static('public'))
4560 là rất quan trọng. Chúng được gọi tuần tự, do đó thứ tự xác định mức độ ưu tiên của phần mềm trung gian. Ví dụ: thông thường, trình ghi nhật ký là phần mềm trung gian đầu tiên bạn sẽ sử dụng để mọi yêu cầu được ghi lại

app.use(express.static('public'))
154

Now suppose you wanted to ignore logging requests for static files, but to continue logging routes and middleware defined after

app.use(express.static('public'))
4561. Bạn chỉ cần di chuyển cuộc gọi đến
app.use(express.static('public'))
4562 lên trên cùng, trước khi thêm phần mềm trung gian logger

app.use(express.static('public'))
155

Một ví dụ khác là phục vụ tệp từ nhiều thư mục, ưu tiên cho “. /public” so với những cái khác

app.use(express.static('public'))
156

Phương pháp

app.use(express.static('public'))
4560 cũng hỗ trợ các tham số được đặt tên để các điểm gắn kết của bạn cho các bộ định tuyến khác có thể hưởng lợi từ việc tải trước bằng các tham số được đặt tên

GHI CHÚ. Although these middleware functions are added via a particular router, when they run is defined by the path they are attached to (not the router). Do đó, phần mềm trung gian được thêm qua một bộ định tuyến có thể chạy cho các bộ định tuyến khác nếu các tuyến của nó khớp. For example, this code shows two different routers mounted on the same path

app.use(express.static('public'))
157

Even though the authentication middleware was added via the

app.use(express.static('public'))
4564 it will run on the routes defined by the
app.use(express.static('public'))
4565 as well since both routers were mounted on
app.use(express.static('public'))
4566. To avoid this behavior, use different paths for each router

Làm cách nào để gửi HTML trong Express?

Bước 1 – Thiết lập dự án. Đầu tiên, mở cửa sổ terminal của bạn và tạo một thư mục dự án mới. mkdir express-sendfile-ví dụ. Sau đó, điều hướng đến thư mục vừa tạo. .
Step 2 – Using res. sendFile() Revisit server. js with your code editor and add path , . get() and res. Gửi file(). người phục vụ. js

Làm cách nào để gửi HTML trong nodejs?

get("/", (req, res) => { res. sendFile(__dirname + "/index. html"); }); Lưu tệp và mở http. //máy chủ cục bộ. 3000/ in the browser, and the server will send a web page as expected.

How can we render plain HTML in Express?

The res. sendFile() phương thức của express. js module is used to render a particular HTML file that is present in the local machine.