LEARNING JAVASCRIPT - Trang 322

times called a verb) and a URL path. You might be surprised to see two requests each

time you go to that URL in the browser:

GET /
GET /favicon.ico

Most browsers will request an icon that they can display in the URL bar or tab; the

browser will do this implicitly, which is why we see it logged on the console.
At the heart of Node’s web server is the callback function that you provide, that will

respond to all incoming requests. It takes two arguments, an

IncomingMessage

object

(often abbreviated

req

) and a

ServerRequest

object (often abbreviated

res

). The

IncomingMessage

object contains all information about the HTTP request: what URL

was requested, any headers that were sent, any data sent in the body, and more. The

ServerResponse

object contains properties and methods to control the response that

will be sent back to the client (usually a browser). If you saw that we called

req.end

and wondered if

req

is a write stream, go to the head of the class. The

ServerRes

ponse

object implements the writable stream interface, which is how you write data

to the client. Because the

ServerResponse

object is a write stream, it makes it easy to

send a file…we can just create a file read stream and pipe it to the HTTP response.

For example, if you have a favicon.ico file to make your website look nicer, you could

detect this request and send this file directly:

const

server

=

http

.

createServer

(

function

(

req

,

res

) {

if

(

req

.

method

===

'GET'

&&

req

.

url

===

'/favicon.ico'

) {

const

fs

=

require

(

'fs'

);

fs

.

createReadStream

(

'favicon.ico'

);

fs

.

pipe

(

res

);

// this replaces the call to 'end'

}

else

{

console.log(

`

${

req

.

method

}

${

req

.

url

}

`

);

res

.

end

(

'Hello world!'

);

}
});

This a minimal web server, though not a very interesting one. With the information

contained in

IncomingRequest

, you can expand this model to create any kind of web‐

site you wish.
If you’re using Node to serve websites, you’ll probably want to look into using a

framework such as

Express

or

Koa

that will take some of the drudgery out of building

a web server from scratch.

Koa is something of a successor to the very popular Express, and

it’s no coincidence: both are the work of TJ Holowaychuk. If you’re

already familiar with Express, you will feel right at home with Koa

—except that you get to enjoy a more ES6-centric approach to web

development.

298 | Chapter 20: Node

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.