The path to distribute

I’m going to do all of this in the code, so let’s just look at the code

Let’s start with the REQ object

let http = require('http'); let server = http.createServer(); server.on('request', (req, Req => {// Req is an instance of http.IncomingMessag class // this class has a message.url attribute // This attribute can be used to obtain the requested path res.writehead (200, { "Content-Type": "text/plain; Charset =utf-8"}) res.end(' sandy '); console.log(req.url); }) server.listen(666);Copy the code

Take the path, can you determine what the path is, and then go back to the corresponding path

let http = require('http'); let server = http.createServer(); server.on('request', (req, Res) => {// the req object is an instance of the http.IncomingMessag class // this class has a message.url property // this property can get the requested path // print to see if it can get it res.writeHead(200, { "Content-Type": "text/plain; If (req.url.startswith ('/index')) {res.end(' I am the home page '); } else if (req.url.startswith ('/login')) {res.end(' I am login interface '); } else {res.end(' no data '); } }) server.listen(666);Copy the code

Next, look at the RES object

The RES object is an instance of the HTTP. ServerResponse class

The HTTP. ServerResponse class also has write methods that are roughly the same as the RES object

There is one difference

Difference: If the data is returned through the end method, it is returned only once

let http = require('http'); let server = http.createServer(); server.on('request', (req, res) => { res.writeHead(200, { "Content-Type": "text/plain; Charset =utf-8"}) if (req.url.startswith ('/index')) {res.end(' I am home page 1'); Res.end (' I am home interface 2'); } else if (req.url.startswith ('/login')) {res.end(' I am login interface '); } else {res.end(' no data '); } }) server.listen(666);Copy the code

Then try writing

let http = require('http'); let server = http.createServer(); server.on('request', (req, res) => { res.writeHead(200, { "Content-Type": "text/plain; Charset =utf-8"}) if (req.url.startswith ('/index')) {res.write(' I am home interface 1'); Res.write (' I am home page 2'); } else if (req.url.startswith ('/login')) {res.end(' I am login interface '); } else {res.end(' no data '); } }) server.listen(666);Copy the code

Aye? Does the web page keep turning? Unable to respond to

Why is that?

Since write can only return data,end can both end the request and return data

If the data is returned multiple times, the end method should be followed

let http = require('http'); let server = http.createServer(); server.on('request', (req, res) => { res.writeHead(200, { "Content-Type": "text/plain; Charset =utf-8"}) if (req.url.startswith ('/index')) {res.write(' I am home interface 1'); Res.write (' I am home page 2'); res.end(); } else if (req.url.startswith ('/login')) {res.end(' I am login interface '); } else {res.end(' no data '); } }) server.listen(666);Copy the code

Is there no problem