nodejs-服务器端

1、创建web服务器

// 引用系统模块
const http = require('http');
// 创建web服务器
const app = http.createServer();
// 当客服端发送请求时
app.on('request',(req,res)=>{
	// 响应
	res.end('<h2>hello user</h2>')
});
// 监听3000端口
app.listen(3000);
console.log('网站服务器启动成功')

在命令行下先执行该js文件,执行成功之后在浏览器端输入localhost:3000

2、http协议–超文本传输协议

1、请求报文-
  • 请求方式:1、get 2、post
  • 获取请求方式 req.method
  • 获取请求地址 req.url
  • 获取报文信息 req.headers
2、响应报文
  • http状态码
    1. 200 请求成功
    2. 404 请求资源找不到
    3. 500 服务器端错误
    4. 400客户端请求有语法错误
res.writeHead(状态码)
  • 内容类型
    1. text/html
    2. text/css
    3. application/javascript
    4. image/jpeg
    5. application/json
res.writeHead(状态码,{
'content-type':'text/plain;charset:utf8' //纯文本
})

3、http请求与响应处理

1、get请求参数
比如:http://localhost:3000/?name=zhangsan&age=20

使用node提供的内置模块–url

1 引入url
2 看效果console.log(req.url)
3 解析url地址、true代表解析成对象  
  console.log(url.parse(req.url,true)
4 获取对象
	let params =url.parse(req.url,true).query
	console.log(params.name)
	console.log(params.age)
2、post请求参数

参数被放置在请求体中进行传输

获取post参数需要使用data事件和end事件

使用querystring系统模块将参数转换为对象格式

post.js

// 引用系统模块
const http = require('http');
// 创建web服务器
const app = http.createServer();
// 处理请求参数模块
const querystring = require('querystring');
// 当客服端发送请求时
app.on('request',(req,res)=>{
	// post参数是通过事件的方式接受的
	// data 当请求参数传递的时候发出data事件
	// end 当参数传递完成的时候发出end事件
	
	let postParams=';'
	req.on('data',params =>{
		postParams+=params;
	});
	req.on('end',()=>{
		console.log(querystring.parse(postParams));
	});
	res.end('ok');
});
// 监听3000端口
app.listen(3000);
console.log('网站服务器启动成功')

form.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>表单提交</title>
	</head>
	<body>
		<form action="http://localhost:3000" method="post">
			<input type="text" name="user" />
			<input type="password" name="password" />
			<input type="submit" value="提交"/>
		</form>
	</body>
</html>

4、路由

请求什么响应什么

路由实现

// 1 引入系统模块
// 2 创建网站服务器
// 3 为网站服务器对象添加请求事件
// 4 实现路由功能
// 1 获取请求方式
// 2 获取请求地址
const http = require('http');
const app = http.createServer();
const url = require('url');
app.on('request', (req, res) => {
	const method = req.method.toLowerCase();
	const pathname = url.parse(req.url).pathname;
	// 响应报文的数据
	res.writeHead(200,{
		'content-type':'text/html;charset=utf8'
	})
	if (method == 'get') {
		if (pathname == '/' || pathname == '/index') {
			res.end("欢迎来到首页");
		} else if (pathname == '/list') {
			res.end('欢迎来打列表页')
		} else {
			res.end('访问出错')
		}
	} else if (method == 'post') {

	}
})
// 监听3000端口
app.listen(3000);
console.log('网站服务器启动成功')

5、静态资源访问


const http = require('http');
const app = http.createServer();
const url = require('url');
const path = require('path');
const fs = require('fs')
// 解决网站中不出现content-type
const mime=require('mime');
app.on('request', (req, res) => {
	// 获取用户请求路径
	let pathname = url.parse(req.url).pathname
	// 默认路径地址
	pathname=pathname=='/'?'/default.html':pathname;
	// 将用户的请求路径转换为实际服务器硬盘的路径
	let realPath=(path.join(__dirname,'public'+pathname))
	// 解决网站中不出现content-type
	let type=mime.getType(realPath)
	// 读取文件
	fs.readFile(realPath,(error,result)=>{
		
		// 文件读取失败
		if(error!=null){
			res.writeHead(404,{
				'content-type':'text/html;charset=utf8'
			})
			res.end('文件读取失败')
			return;
		}
		// 解决网站中不出现content-type
		res.writeHead(200,{
			'content-type':type
		})
		res.end(result)
	})
})
// 监听3000端口
app.listen(3000);
console.log('网站服务器启动成功')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值