介绍:express是基于Node.js平台,快速,开放,极简的WEB开发框架
npm install express
const exprsss=require('express')
express是一个第三方模块所以会自动去node_modules文件夹里面去找
const express=require('express')
// 导入express模块
console.log(express)
// 打印显示
如果结果为一串很长的对象就是成功的

const express=require('express')
// 导入express模块
const app = express();
// 创建web服务器
app.listen(3000,()=>{
console.log('http://127.0.0.1:3000 and http://localhost:3000');
})
开启一个端口为3000的本地服务器
app.listen(3000,()=>{
console.log('http://127.0.0.1:3000 and http://localhost:3000');
}
后面的括号第一个参数为端口第二个为创建成功返回字符
一个端口理论上只能一个接口或者页面使用,但是我们一般不止也个页面,所以我们为了多个页面和功能能正常使用所以我们可以使用路由
根据不同的请求来进行处理,格局不同的请求来挂载特定的页面,就像家里的网络一样的,原本一条网线就只能链接一台设备,我们要让更多设备同时上网就要外接路由器,路由给他们分配不同的IP地址,比如管理页面192.168.1.1,手机为192.168.1.32,这个道理
总结:监听特定的请求
比如我们监听对首页的请求
请求的网址:/index 请求的方式:get
// 监听对首页的请求
// 请求的网址:/index 请求的方式:get
app.get('/', (req, res)=>{
console.log('hello')
//当有请求之后执行的代码
// res响应的对象,和HTTP模块的有所不同
// 设置响应的内容并发送
res.send('hello world');
});
添加/list和/home访问后分别是列表和主页
浏览器


控制台

多写几个路由来实现两个,利用 res.send(‘{{text}}’);和app.get来实现
const express=require('express')
// 导入express模块
const app = express();
// 创建web服务器
app.listen(3000,()=>{
console.log('http://127.0.0.1:3000 and http://localhost:3000');
});
// 监听对首页的请求
// 请求的网址:/index 请求的方式:get
app.get('/list', (req, res)=>{
// res响应的对象,和之前的不一样
// 设置响应的内容并发送
console.log('list');
res.send('列表');
})
app.get('/home', (req, res)=>{
// res响应的对象,和之前的不一样
// 设置响应的内容并发送
console.log('home');
res.send('主页');
})
顾名思义就是跳转举个简单的例子,假如我创建好之后,我什么都不写就是很尴尬,但是我们利用重定向就能完美解决

为了解决这个尴尬我们就可以使用重定向
res.redirect('重定向网址')
比如:
我访问纯网站时直接跳到home页面
app.get('/', (req, res)=>{
// res响应的对象,和之前的不一样
// 设置响应的内容并发送
console.log('home');
res.redirect('http://localhost:3000/home')
});

如图当我访问http://127.0.0.1:3000 或者 http://localhost:3000就会自动跳转到http://localhost:3000/home
如:我访问/code时间响应index.html
这个时候就要用到res.sendFile
res.sendFile('文件路径')


但是我们这样写了之后他是报错的
大意是不能为相对路径,应该为绝对路径
还有一个办法就是通过 '__dirname’来获得绝对路径

所以


所以代码应该为:
app.get('/code', function(req, res) {
res.sendFile(__dirname + '/app.js');
// 此处应该为绝对路径
})
我们访问http://localhost:3000/code则为

const express=require('express')
// 导入express模块
const app = express();
// 创建web服务器
app.listen(3000,()=>{
console.log('http://127.0.0.1:3000 and http://localhost:3000');
});
// 监听对首页的请求
// 请求的网址:/index 请求的方式:get
app.get('/list', (req, res)=>{
// res响应的对象,和之前的不一样
// 设置响应的内容并发送
console.log('list');
res.send('列表');
})
app.get('/home', (req, res)=>{
// res响应的对象,和之前的不一样
// 设置响应的内容并发送
console.log('home');
res.send('主页');
})
app.get('/', (req, res)=>{
// res响应的对象,和之前的不一样
// 设置响应的内容并发送
console.log('home');
res.redirect('http://localhost:3000/home')
})
app.get('/code', function(req, res) {
res.sendFile(__dirname + '/app.js');
// 此处应该为绝对路径
})
// 获取绝对路径
console.log(__dirname + '/app.js')
我们要实现一个搜索,首先我们先要得到一个搜索引擎的api请求地址这边我直接拿来一个360的接口
https://www.so.com/s?ie=utf-8&src=hao_360so_suggest_b_cube&shb=1&hsid=ead9b5d97dc6b413&eci=undefined&nlpv=yc&ssid=4eb390e734b0444e96ed2bc57d151b7f&q=xxx
我后面标注的xxx就是参数
然后原理就是本地提交数据,然后app.js接收数据然后跳转,接下来我们来试试
html页面使用for表单提交核心代码为
<form method="get" action="http://localhost:3000/search">
<input
style="
color: rgb(1, 78, 78);
width: 30vw;
height: 40px;
border-radius: 10px;
border: 1px solid rgb(194, 191, 191);
"
type="text"
name="text"
id="111"
/>
<input
style="
color: bisque;
background-color: rgb(37, 83, 122);
width: 10vw;
height: 40px;
border-radius: 10px;
border: 1px solid rgba(194, 191, 191, 0);
"
type="submit"
value="搜索"
/>
</form>
提交地址为本地的"http://localhost:3000/search"
<!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>
</head>
<body style="background-color: rgb(112, 112, 112)">
<div>
<h1
style="
user-select: none;
text-align: center;
font-size: 40px;
color: rgb(21, 96, 146);
"
>
达内搜索
</h1>
</div>
<div
style="
margin-top: 15vh;
text-align: center;
font-size: 30px;
color: rgb(21, 96, 146);
"
>
<form method="get" action="http://localhost:3000/search">
<input
style="
color: rgb(1, 78, 78);
width: 30vw;
height: 40px;
border-radius: 10px;
border: 1px solid rgb(194, 191, 191);
"
type="text"
name="text"
id="111"
/>
<input
style="
color: bisque;
background-color: rgb(37, 83, 122);
width: 10vw;
height: 40px;
border-radius: 10px;
border: 1px solid rgba(194, 191, 191, 0);
"
type="submit"
value="搜索"
/>
</form>
</div>
</body>
</html>
html页面使用for表单提交核心代码为
app.get('/search', function(req, res) {
//查看接收到的数据
console.log(req.query.text)
// 拼接跳转
res.redirect(`https://www.so.com/s?ie=utf-8&src=hao_360so_suggest_b_cube&shb=1&hsid=ead9b5d97dc6b413&eci=undefined&nlpv=yc&ssid=4eb390e734b0444e96ed2bc57d151b7f&q=${req.query.text}`)
})
接受参数的接口为"/search"
const express=require('express')
// 导入express模块
const app = express();
// 创建web服务器
app.listen(3000,()=>{
console.log('http://127.0.0.1:3000 and http://localhost:3000');
});
app.get('/search', function(req, res) {
// console.log(req.query);
// 此处应该为绝对路径
res.redirect(`https://www.so.com/s?ie=utf-8&src=hao_360so_suggest_b_cube&shb=1&hsid=ead9b5d97dc6b413&eci=undefined&nlpv=yc&ssid=4eb390e734b0444e96ed2bc57d151b7f&q=${req.query.text}`)
})
// 获取绝对路径
console.log(__dirname + '/app.js')
主要使用的模块的{{req}}功能【接收的数据】
参数总结
