当前位置:网站首页>---static page---
---static page---
2022-08-02 03:42:00 【cjx177187】
01.Html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./01.css">
</head>
<body>
<img src="./1.png" >
<img src="https://p3.ssl.qhimg.com/dmfd/400_300_/t0140582dc583d923bf.png" >
<a href="./02.html">网页2</a>
<a href="https://www.baidu.com">百度</a>
</body>
</html>
node.js代码
var http=require("http")
var fs=require("fs")
var app=http.createServer((req,res)=>{
if(req.url=="/01.html"){
fs.readFile(__dirname+"/01.html",(err,data)=>{
res.end(data)
})
}else if(req.url=="/1.jpg"){
fs.readFile(__dirname+"/1.jpg",(err,data)=>{
res.end(data)
})
}else if(req.url=="/02.html"){
fs.readFile(__dirname+"/02.html",(err,data)=>{
res.end(data)
})
}
})
app.listen(8081)
When we enter the URL in the browser,Received is the source code of the webpage,How does this code browser work?,It runs line by line from top to bottom;Parse nodes in a document tree,A node-by-node parsing,当解析到 <link rel="stylesheet" href="./01.css">代码时,It will spontaneously make a request to the backend,再次运行http.createServer这个函数,This is the browser will receive a copycss代码,然后再渲染到页面上,再回到HTMLRun the next line of code on the file,一直运行到
<img src="./1.png" > 时 再次 发送请求调用http.createServer,获取1.pngThis image is rendered on the page,Execute line by line like this,Time and time again to send the request to accept data,The last page we see.
但是我们会发现,Our code needs to check again and again to determine the network address we are requesting and whether we are what we want,If we are using
fs.readFilefunction to read data and send it to the front end,In this way, we need to write a web page many timesif和else语句,And many lines of code are actually similar,It only changes our judgment conditions and the data to be read.We will also find a little,That is, the statement we are going to judge is actually the same as the file name we want to read first, but an absolute path is added before the statement we want to read..
So we can try to imagine why we can't put the judgment statement and the read file path as a variable into our code.,This replaces all of ourif判断了
改进:
var http=require("http")
var fs=require("fs")
var app=http.createServer((req,res)=>{
fs.readFile(__dirname+req.url,(err,data)=>{
res.end(data)
})
})
app.listen(8081)
边栏推荐
猜你喜欢
Problems when yolov5 calls ip camera
js基础知识
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine supply, MW: 5000
Phospholipid-polyethylene glycol-thiol, DSPE-PEG-Thiol, DSPE-PEG-SH, MW: 5000
Redis simple study notes
Cut out web icons through PS 2021
配置mmdet来训练Swin-Transformer之一配置环境
canvas--饼状图
require modular syntax
DOM manipulation---magnifying glass case
随机推荐
js takes the value of a feature at a certain position in the string, such as Huawei=> Huawei
啃瓜记录又一天
每天填坑,精卫填坑第二集,TX1 配置从固态启动,安装Pytorch
Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000
kettle 安装与配置
C语言 内联函数
SSM integration
Cut out web icons through PS 2021
L1-043 阅览室 (20分)
npm and package.json
Knowledge Engineering Assignment 2: Introduction to Knowledge Engineering Related Fields
parser = argparse.ArgumentParser() parsing
js basics
[Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)
js基础知识
枚举法方法:(leetcode1300)转变数组后最接近目标值的数组和
Redis简单学习笔记
骨架效果 之高级渐变,适用图片等待时
针对简历上的问题
canvas--pie chart