当前位置:网站首页>Node访问服务器端静态资源
Node访问服务器端静态资源
2022-04-23 05:53:00 【遥岑.】
通过Node访问服务器端静态资源
通过Node的http模块访问服务器端的静态资源(css文件、图像、html文件)。
- 获取客户端请求的url地址
//获取用户请求路径
console.log(req.url)
- 找到url对应的资源文件,利用fs模块读取文件的内容
- 将文件内容发送给客户端
if(req.url === '/index' || req.url ==='/') {
//读到的就是Buffer型数据
fs.readFile(path.join(__dirname,'index.html'),function (err,data) {
if(err) {
throw err
}
res.end(data)
})
}else {
res.end('404')
}
//根据不同请求 做出不同响应(HTML文件)
var http = require('http')
var fs = require('fs')
var path = require('path')
http.createServer(function (req,res) {
// res.setHeader('Content-Type','text/html;charset = utf-8')
if(req.url === '/index' || req.url ==='/') {
//读到的就是Buffer型数据
fs.readFile(path.join(__dirname,'index.html'),function (err,data) {
if(err) {
throw err
}
res.end(data)
})
}else {
res.end('404')
}
}).listen(8080,function () {
console.log('服务器启动')
})
mime.getType(url)
可以通过路径返回资源类型。
var http = require('http')
var path = require('path')
var fs = require('fs')
var mime = require('mime')
http.createServer(function (req,res) {
//获取images下的路径
var imagesDir = path.join(__dirname,'images')
// console.log(imagesDir)
//计算用户请求的完整路径
var filename = path.join(imagesDir,req.url)
console.log(filename)
//读文件
fs.readFile(filename,function (err,data) {
if(err) {
res.setHeader('Content-Type','text/plain;charset=utf-8')
res.end('未找到文件')
} else {
console.log(filename)
//mime自动匹配文件类型
res.setHeader('Content-Type',mime.getType(filename))
res.end(data)
}
})
}).listen(8080,function () {
console.log('服务器启动')
})
res的常用属性
- 设置响应报文头
res.setHeader('Content-Type','text/plain;charset=utf-8')
- 设置http状态码
res.statusCode = 404
res.statusMessage = 'Not Found'
- 写入http响应报文头
res.writeHead(200,'OK',{
'Content-Type':'text/plain;charset=utf-8'
})
- 响应的报文头报文体已经响应完毕
res.end('end')
//res常用属性
var http = require('http')
http.createServer(function (req,res) {
//设置响应报文头
// res.setHeader('Content-Type','text/plain;charset=utf-8')
//设置http状态码
// res.statusCode = 404
// res.statusMessage = 'Not Found'
//直接向客户端响应(写入)http响应报文头 可代替上面的
res.writeHead(200,'OK',{
'Content-Type':'text/plain;charset=utf-8'
})
//res.write()
res.write('hello 你好')
//告诉服务器该响应的报文头报文体已经响应完毕,可以考虑本次响应结束
res.end('end')
}).listen(8080,function () {
console.log('服务器启动')
})
req的常用属性
- 获取所有的请求报文头,返回的是一个对象
console.log(req.headers)
- 获取请求报文头,返回的是一个数组
console.log(req.rawHeaders)
- 获取请求客户端所使用的http版本
console.log(req.httpVersion)
- 获取客户端请求的方法
console.log(req.method)
- 获取请求的路径
console.log(req.url)
//req常用属性
var http = require('http')
http.createServer(function (req,res) {
//获取所有的请求报文头 返回的是一个对象
// console.log(req.headers)
//返回一个数组,数组中保存的是请求报文头的字符串
// console.log(req.rawHeaders)
//httpVersion获取请求客户端所使用的http版本
// console.log(req.httpVersion)
//获取客户端请求的方法
// console.log(req.method)
//获取请求的路径
console.log(req.url)
res.end('end')
}).listen(8080,function () {
console.log('服务器启动')
})
版权声明
本文为[遥岑.]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_52942098/article/details/120918325
边栏推荐
- [UDS unified diagnostic service] i. overview of diagnosis (4) - basic concepts and terms
- Qt 添加QSerialPort类 实现串口操作
- VHDL arbitrary frequency divider (50% duty cycle)
- 赛氪-二进制
- realsense 选型大对比D455 D435i D415 T265 3D硬件对比
- 说说ts的心里话
- [opencv] use filestorage to read and write eigenvectors
- Sdoi2009-hh Necklace
- C语言结构体指定初始化
- ES6
猜你喜欢
Makefile基础、常用函数及通用Makefile
QT add qserialport class to realize serial port operation
Collection of practical tips for C language (continuously updated)
FOC电机库 定点PID代码分析
[UDS unified diagnostic service] IV. typical diagnostic service (2) - data transmission function unit
Analysis and setting of dead time
基于SSD的物体检测案例实现
uniapp 自定义搜索框适配小程序对齐胶囊
Error in created hook: “ReferenceError: “Promise”未定义“
汇编 32位无符号加法计算器
随机推荐
Quaternion multiplication
Informatics one book pass - small ball
提交本地仓库并同步码云仓库
Qt 添加QSerialPort类 实现串口操作
v-for下定时给图片添加动画
Assembly base code example
SSH 公钥 私钥的理解
Using printf in MFC
Multibyte and Unicode in VS
[UDS unified diagnostic service] IV. typical diagnostic service (5) - function / component test function unit (routine function unit 0x31)
赛氪-zeal
FOC SVPWM函数PWMC_SetPhaseVoltage解析
信息学一本通-小球
VHDL-任意分频器(50%占空比)
js获取链接?后边的参数名称或者值,根据url ?后的参数做判断
TensorFlow张量介绍
[UDS unified diagnostic service] i. overview of diagnosis (4) - basic concepts and terms
uniapp 自定义搜索框适配小程序对齐胶囊
算数表达式
谈谈v-if显示隐藏问题