js文件
// 编写js脚本文件 创建服务器 添加路由 响应html文件
// 引入模块
const express=require('express')
// 创建服务器
const app=express()
// 设置端口
app.listen(3000,()=>{
console.log('启动成功');
})
// 添加路由
app.get('/search',(req,res)=>{
res.sendFile(__dirname+'/01.html')
})
// 添加路由 监听按钮提交的请求
app.get('/mysearch',(req,res)=>{
console.log(req.method,req.url);
// 获取传递的参数
console.log(req.query.kw);
res.send('搜索成功')
})
html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h3>用户搜索</h3>
<form action="/mysearch" method="get">
<!-- name 用于设置参数名 接收输入的值 -->
<input type="text" placeholder="请输入关键字" name="kw">
<button type="submit">搜索</button>
</form>
</body>
</html>