Node.js returns different data according to different request paths.

Node.js returns different data according to different request paths.

1. Learn to return different data according to different request paths:

var url=req.url //Get the req.url value (req: is the abbreviation of request)
req.url : Get the path after the port number to achieve different paths to return different data

My port number: 3000, URL: http://127.0.0.1:3000

if(url==='/'){

res.end('index page') //If the input URL is: http://127.0.0.1:3000/
//Respond to the data in brackets and send the data to the server for display
}

if(url==='/login')
{

res.end('login page') //If the input URL is: http://127.0.0.1:3000/login
//Respond to the data in brackets and send the data to the server for display
}

insert image description here

var http = require("http"); // http module http.createServer(function(req, res) {
//res.write('hello')
//res.write('world!')
 // res.end('index page');

var url=req.url //Get req.url value if(url==='/'){

res.end('index page') //Content ends}else if(url==='/login')
{

res.end('login page')


}else{

  res.end('404')
}

console.log(req.url);

}).listen(3000); // Listen to port 3000
 
console.log("HTTP server is listening at port 3000. The URL is http://127.0.0.1:3000");

result:

insert image description here

insert image description here

2. Data sent: data type and encoding: Content-Type

res.setHeader('Content-Type','text/plain; charset=utf-8')
res.setHeader('Content-Type','text/html; charset=utf-8')

text/plain: Plain text: If the content is an HTML tag, you need to change it to: text/html
res.end(“helloworld”); Use text/plain

res.end('<p>Who am I<a>Click</a></p>') //Use text/html to be recognized by the browser

charset=utf-8 : The content is encoded in utf-8

insert image description here

3. About reading files: relative path and absolute path:

This relative path is actually relative to the path where the node command is executed:

var fs=require(“fs”) //fs has many API functions, get the fs object
fs.readFile()//Read the file I execute the node command in: d:\node1.js
The file 07.html is in the directory: d:node1.js;
So: fs.readFile('./07.html', funtion(){ })
You can read the file; pass the content to data
Again
res.end(data)
Just write the HTML content in: res.red() and open the web page to see s.end

insert image description here

insert image description here

var http = require("http"); // http module var fs = require("fs")
   //var url=req.url;
http.createServer(function(req, res) {


//res.write('hello')
//res.write('world!')
 // res.end('index page');
 fs.readFile('./07.html',function(err,data) {
    
if(err){

res.setHeader('Content-Type','text/plain; charset=utf-8')

     res.end('wss')

}
else{

res.setHeader('Content-Type','text/html; charset=utf-8')
res.end(data)

}

 })


}).listen(3000);
console.log("service")

result:

insert image description here

4. Read pictures

fs.readFile('./07.jpg',function(err,data)
res.setHeader('Content-Type','image/jpeg; charset=utf-8')
res.end(data)
//Main code

This concludes this article about the detailed process of node.js returning different data according to different request paths. For more relevant node.js request paths and data content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Node.js path processing method and absolute path detailed explanation
  • Detailed explanation of path processing module path in Node.js
  • Teach you how to crawl website data with Node.js crawler
  • Example analysis of crawling Douban data with Node.js

<<:  How to deploy LNMP architecture in docker

>>:  UTF-8 and GB2312 web encoding

Recommend

Examples of common Nginx misconfigurations

Table of contents Missing root location Off-By-Sl...

MySQL 8.0.11 compressed version installation tutorial

This article shares the installation tutorial of ...

Sliding menu implemented with CSS3

Result:Implementation code: <!DOCTYPE html>...

Complete list of CentOS7 firewall operation commands

Table of contents Install: 1. Basic use of firewa...

Install and configure MySQL 5.7 under CentOS 7

This article tests the environment: CentOS 7 64-b...

A Preliminary Study on JSBridge in Javascript

Table of contents The origin of JSBridge The bidi...

Vue3.0 implements encapsulation of checkbox components

This article example shares the specific code of ...

11 common CSS tips and experience collection

1. How do I remove the blank space of a few pixels...

Solve the error problem caused by modifying mysql data_dir

Today, I set up a newly purchased Alibaba Cloud E...

JavaScript implements the nine-grid click color change effect

This article shares the specific code of JavaScri...

Case analysis of several MySQL update operations

Table of contents Case Study Update account balan...

Control the light switch with js

Use js to control the light switch for your refer...

How to deploy SpringBoot project using Docker

The development of Docker technology provides a m...

Detailed explanation of MySQL's Seconds_Behind_Master

Table of contents Seconds_Behind_Master Original ...