node中http模板

网络是传输,接受信息的虚拟平台
http是node的http模块,用于创建http server服务,且支持更多特征

本地创建服务器

基础用法

在这里插入代码片const http = require('http');//引入http模块

let server = http.createServer();//创建一个http服务器
server.on('request',(req,res)=>{//绑定事件
	res.write('hello word');
	res.end();
});
server.listen(80,()=>{//监听端口
	console.log('server in running')
})

简写与上一种方法相同

const http = require('http');//引入http模块

http.createServer((req,res)=>{
	res.write('hello word');
	res.end();
}).listen(80,()=>{
	console.log('Server in Running');
	console.log('http://127.0.0.1')
})

响应头

状态码
http状态码是用来表示网页服务器响应状态的三位数字代码
状态码非常多,用的到的就这几个,标重点的是常用的## 标题

  • 1xx:表示普通消息
  • 2xx:表示服务器响应成功
  •  	200(响应成功)【重】
    
  • 3xx:表示重定向
  •  	301(永久重定向)【重】
    
  •  	302(零时重定向)
    
  •  	304(使用缓存)
    
  • 4xx(无法访问)
  •  	403(权限不足,无法访问)
    
  •  	404(找不到资源)【重】
    
  • 5xx(服务器有错)
  •  	500(服务器端代码有错)【重】
    
  •  	502(网关错误)
    
  •  	503(服务器崩溃)
    

mine类型
简单的说mine是一个互联网标准,通过设定他可以规定不同类型文件在浏览器中以何种方式打开
找到了几个常用的对应的key值为文件后缀名,value值为对应的mine类型

const mimeTypes = {
    '.css': 'text/css',
    '.gif': 'image/gif',
    '.html': 'text/html',
    '.ico': 'image/x-icon',
    '.jpeg': 'image/jpeg',
    '.jpg': 'image/jpeg',
    '.js': 'text/javascript',
    '.json': 'application/json',
    '.pdf': 'application/pdf',
    '.png': 'image/png',
    '.svg': 'image/svg+xml',
    '.swf': 'application/x-shockwave-flash',
    '.tiff': 'image/tiff',
    '.txt': 'text/plain',
    '.wav': 'audio/x-wav',
    '.wma': 'audio/x-ms-wma',
    '.wmv': 'video/x-ms-wmv',
    '.xml': 'text/xml'
};

了解了状态码,和mine类型,它们都是写在响应头中的
响应头中一般还要写入charset属性规定HTML文档的字符编码,避免文字乱码

res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"})//写响应头res.writeHead(状态码,{规定mine编码})

实现不同请求返回不同数据

默认不设置文件名中,任何url的文件名都跳转同一页面

const http = require('http');//引入http模块

http.createServer((req,res)=>{
	if (req.url === '/') {
		res.write('<h1>hello word</h1>');
		res.end();
	}else if(req.url==='/new.html'){
		res.write('<h1>news</h1>');
		res.end();
	}else if(req.url === '/about.html'){
		res.write('<h1>我的</h1>');
		res.end();
	}
}).listen(80,()=>{
	console.log('Server in Running');
	console.log('http://127.0.0.1')
})

实现不同的文件名跳转至不同的页面

制作一个静态网页服务器

const http = require('http');//引入http模块
const path = require('path');//引入路径模块
const fs = require('fs');//引入fs模块


http.createServer((req,res)=>{
	if (req.url === '/') {
		req.url = '/index.html';//制造一个默认的主页面
	};
	let urlS = path.join(__dirname,'..',req.url);//整理路径,需要根据本身的html位置决定
	let ext = path.parse(urlS).ext;//取得文件的后缀名
	let mineExt = { //写mine类型,可以通过得到的后缀名获得具体的状态码
	 	'.css': 'text/css',
	    '.gif': 'image/gif',
	    '.html': 'text/html',
	    '.ico': 'image/x-icon',
	    '.jpeg': 'image/jpeg',
	    '.jpg': 'image/jpeg',
	    '.js': 'text/javascript',
	    '.json': 'application/json',
	    '.pdf': 'application/pdf',
	    '.png': 'image/png',
	    '.svg': 'image/svg+xml',
	    '.swf': 'application/x-shockwave-flash',
	    '.tiff': 'image/tiff',
	    '.txt': 'text/plain',
	    '.wav': 'audio/x-wav',
	    '.wma': 'audio/x-ms-wma',
	    '.wmv': 'video/x-ms-wmv',
	    '.xml': 'text/xml'};
	fs.readFile(urlS,(err,data)=>{//读对应的html文件
		if (err) {
			res.writeHead(404,{'Content-Type':'text/html;charset=utf-8'});
			res.write('找不到数据');
			res.end();
		} else {
			res.writeHead(200,{'Content-Type':mineExt[ext]+';charset=utf-8'});
			res.write(data);
			res.end();
		}
	})

}).listen(80,()=>{
	console.log('Server in Running');
	console.log('http://127.0.0.1')
})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值