根据用户不同请求-读取不同HTML文件响应|学习笔记

简介: 快速学习根据用户不同请求-读取不同HTML文件响应。本节课还是根据用户不同请求,做出不同的反应,只是这次不同响应不是直接写的,而是根据html网页来实现的。

开发者课程【Node.js 入门与实战:根据用户不同请求-读取不同HTML文件响应 】学习笔记,与课程紧密联系,让用户快速学习知识

课程地址:https://developer.aliyun.com/learning/course/588/detail/8260


根据用户不同请求-读取不同HTML文件响应

本节课还是根据用户不同请求,做出不同的反应,只是这次不同响应不是直接写的,而是根据html网页来实现的。

代码设置

//根据用户不同请求,做出不同响应(响应现有的 HTML 文件)

//加载 http模块

var http = require( 'http');

//加载fs模块

var fs = require( 'fs');

//加载path模块

var path = require( ' path ');

//创建http服务,并启动该服务

http .create Server(function (req, res) {

//通过 req.url 获取用户请求的路径,根据不同的请求路径服务器做出不同的响应 if (req.url ==‘/'req.url === '/index'){

//读取index.html文件

fs. Read File(path .join(__dir name, 'htmls', 'index.html'),function (err, data){});

}else if (req.url == '/login '){1/读取index.html文件

fs. Read File(path  .join(__dir name, 'htmls', 'login.html'),function (err, data) {});

}else if (req.url =a= '/list'){//读取index.html文件

fs. Read File(path .join(__dir name,'htmls', 'list.html'),function (err, data){巴});

}else if (req.url =s '/register'){//读取index.html文件

fs. Read File(path. join(_ dir name,'htmls ', 'register.html'), function (err, data)});

}else i

//读取index.html文件

Fs .read File(path .join(__dir name,'htmls', '404.html' ),function (err, data){});

//通过 req.url 获取用户请求的路径,根据不同的请求路径服务器做出不同的响应

if (req.url === '/'1 req.url === '/index ' ){

//读取 index.html文件

fs. Read File(path .join(__dir name ,'html s ', 'index.html'), function (err ,data){if (err) {throw err;

//把读取到的index.html中的内容直接发送给浏览器res .end(data);

});

}else if (req.url === '/ login') i

//读取index.html文件

fs. Read File(path .join(_ dir name ,'html s', 'login.html'), function (err ,data){if (err) i

throw err;

//把读取到的index.html中的内容直接发送给浏览器res .end(data);

});

}else if (req.url === '/list'){

//读取index.html文件

fs. Read File(path .join(_ dir name, 'html s', 'list.html'),function (err, data)

{

if (err){

throw err;}

//把读取到的index.html中的内容直接发送给浏览器res .end(data);

});

}else if (req.url === '/register'){

//读取index.html文件

fs. Read File(path .join(__dir name, 'html s', 'register.html'),function (err, data){if (err){

throw err;}

//把读取到的index.html中的内容直接发送给浏览器res .end(data);

});

}else {

//读取index.html文件

fs. Read File(path .join(__dir name, 'html s ', 'index.html '),function (err, data)

{

if (err){

l throw err;}

//把读取到的index.html中的内容直接发送给浏览器res. end(data);

});

}

}).listen(9090,function ( {

console.log( 'http: / / localhost : 9090');});

例1、

<!DOCTYPE html><html lang="en"><head>

<meta charset="UTF-8">

<title>Hello Index</title></head>

<body>

<h1 style="color: red; ">Hello Index</h1>

<h1>你好首页!!!看乱码吗?</h1>

</body>

</html>

执行结果:

Hello lndex(此处字体为红色)

你好首页!!!看乱码吗?

注意:

在执行代码时,发现执行结果是空白页面,首先应该查看浏览器,浏览器没有问题再使用工具看是否有发起此请求,如果浏览器有发起响应,注意看服务器是不是有返回响应。

相关文章
|
2天前
|
前端开发
【HTML专栏2】VSCode的使用(新建HTML文件)
【HTML专栏2】VSCode的使用(新建HTML文件)
26 0
|
2天前
|
Java
有关Java发送邮件信息(支持附件、html文件模板发送)
有关Java发送邮件信息(支持附件、html文件模板发送)
54 1
N..
|
2天前
|
移动开发 前端开发 JavaScript
HTML文件
HTML文件
N..
14 1
|
2天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
【5月更文挑战第10天】BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
21 1
|
2天前
|
缓存 JavaScript 前端开发
html文件压缩
【4月更文挑战第28天】html文件压缩
32 5
|
2天前
|
JSON JavaScript 数据格式
python遍历目录文件_结合vue获取所有的html文件并且展示
python遍历目录文件_结合vue获取所有的html文件并且展示
10 0
|
2天前
|
移动开发 JavaScript 前端开发
webgl学习笔记3_javascript的HTML DOM
webgl学习笔记3_javascript的HTML DOM
19 0
webgl学习笔记3_javascript的HTML DOM
|
2天前
【代码片段】【HTML】弹出对话框点选加载文件
【代码片段】【HTML】弹出对话框点选加载文件
16 1
|
2天前
|
Python
python html(文件/url/html字符串)转pdf
python html(文件/url/html字符串)转pdf
10 0
|
2天前
|
数据采集 JavaScript 前端开发
使用 cheerio 解析本地 html 文件
使用 cheerio 解析本地 html 文件
26 1