1.创建http服务---在9000端口打开
// 1.导入http模块
const http = require('http');
// 2.创建服务对象
const server=http.createServer((request,response)=>{
response.end('hello world');
});
// 3.监听端口,启动服务
// (1.端口号,2.回调函数=》服务启动成功后的操作)
server.listen(9000,()=>{
console.log('服务启动成功');
});
使用node命令启动服务,以下是结果
使用‘Ctrl+C’停止端口服务
1.2 设置响应体格式--解决中文字符串乱码问题
使用setHeader方法设置utf-8,解决乱码问题
// 1.导入http模块
const http = require('http');
// 2.创建服务对象
const server=http.createServer((request,response)=>{
response.setHeader('Content-Type','text/html;charset=utf-8');
response.end('你好,NodeJS');
});
// 3.监听端口,启动服务
// (1.端口号,2.回调函数=》服务启动成功后的操作)
server.listen(9000,()=>{
console.log('服务启动成功');
});
结果
2.HTTP服务默认是80端口,也可以用1.的方法指定端口进行访问
常用端口为:3000 8080 9000
3. 浏览器查看HTTP报文
仍然是1.中启动的9000端口,写一个html文件向9000端口发送请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="http://localhost:9000" method="post">
<input type="text" name="username" id="">
<input type="password" name="password" id="">
<input type="submit" value="提交">
</form>
</body>
</html>
成功显示响应结果
4.获取请求体和请求头
// 1.导入http模块
const http = require('http');
// 2.创建服务对象
const server=http.createServer((request,response)=>{
response.setHeader('Content-Type','text/html;charset=utf-8');
// 获取请求的方法
console.log(request.method);
// 获取请求的路径
console.log(request.url);
// 获取请求的HTTP版本
console.log(request.httpVersion);
// 获取请求的请求头
console.log(request.headers);
// 响应体
response.end('NodeJS');
});
// 3.监听端口,启动服务
// (1.端口号,2.回调函数=》服务启动成功后的操作)
server.listen(9000,()=>{
console.log('服务启动成功');
});
5.提取报文请求体
// 1.导入http模块
const http = require('http');
// 2.创建服务对象
const server=http.createServer((request,response)=>{
let body='';
request.on('data',chunk=>{
body+=chunk;
});
request.on('end',()=>{
console.log(body);
});
response.end('hello world');
});
// 3.监听端口,启动服务
// (1.端口号,2.回调函数=》服务启动成功后的操作)
server.listen(9000,()=>{
console.log('服务启动成功');
});
6.获取url中的参数
// 1.导入http模块
const http = require('http');
// 2.导入url模块
const url = require('url');
// 2.创建服务对象
const server=http.createServer((request,response)=>{
//解析url
// console.log(request.url);
let req=url.parse(request.url);
// console.log(req);
let pathname=req.pathname;
console.log(pathname);
response.end('hello world');
});
// 3.监听端口,启动服务
// (1.端口号,2.回调函数=》服务启动成功后的操作)
server.listen(9000,()=>{
console.log('服务启动成功');
});
结果:
7.练习
get请求
// 1.导入http模块
const http = require('http');
// 2.创建服务对象
const server=http.createServer((request,response)=>{
let {method}=request;
// 获取请求的url
let {pathname}=new URL(request.url,'http://localhost:9000');
console.log(method,pathname);
if(method==='GET'&&pathname==='/login'){
response.end('登录页');
}else if(method==='GET'&&pathname==='/reg'){
response.end('注册页');
}
});
// 3.监听端口,启动服务
// (1.端口号,2.回调函数=》服务启动成功后的操作)
server.listen(9000,()=>{
console.log('服务启动成功');
});
8.设置响应报文
// 1.导入http模块
const http = require('http');
// 2.创建服务对象
const server=http.createServer((request,response)=>{
// 设置响应状态吗(默认为200)
response.statusCode=203;
// 设置响应状态的描述
response.statusMessage='csq';
// 设置响应的头
response.setHeader('Content-Type','text/html;charset=utf-8');
response.setHeader('Server','csq');
response.setHeader('Date',['a','b','c']);
// 响应体的设置(write和end方法二选一)
response.write('i am write');
response.end('i am end');
});
// 3.监听端口,启动服务
// (1.端口号,2.回调函数=》服务启动成功后的操作)
server.listen(9000,()=>{
console.log('服务启动成功');
});
9.使用fs模块,将外部文件导入nodejs文件中
// 1.导入http模块
const http = require('http');
// 使用fs倒入外部html文件
const fs = require('fs');
// 2.创建服务对象
const server=http.createServer((request,response)=>{
let html=fs.readFileSync(__dirname+'/table.html','utf-8');
response.end(html);
// response.end('<table border="1"><tr><td>test</td></tr></table>');
});
// 3.监听端口,启动服务
// (1.端口号,2.回调函数=》服务启动成功后的操作)
server.listen(9000,()=>{
console.log('服务启动成功');
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
td{
padding: 20px 40px;
}
table tr:nth-child(odd){
background-color: rgba(252, 204, 252, 0.13);
}
table tr:nth-child(even){
background-color: #ffb9b9ff;
}
table,td{
border-collapse: collapse;
}
</style>
</head>
<body>
<table border="1">
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
</table>
<script>
let tds=document.querySelectorAll('td');
tds.forEach(item=>{
item.onclick=function(){
this.style.backgroundColor='#ff0000';
}
})
</script>
</body>
</html>
10. nodejs模块化:根据功能模块拆分
·高复用
·高维护性
·防止命名冲突
使用对象进行暴露