http模块
话不多说,先上代码
先写一个main.js
// 引入 http 模块
var http = require("http");
// 法创建服务器,并使用 listen 方法绑定 3000 端口
http.createServer(function (request, response) {
// 函数通过 request, response 参数来接收和响应数据。
// 发送 HTTP 头部
// HTTP 状态值: 200 : OK
// 设置 HTTP 头部,状态码是 200,文件类型是 html,字符集是 utf8
response.writeHead(200, { 'Content-Type': 'text/plain' });
// 发送响应数据 "Hello World"
response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
然后在终端运行 node main.js
即可在http://127.0.0.1:3000/的页面中显示 Hello World
这就是一个简单的node.js 内置模块http的简单案例
常用的Content-Type
文件类型 | 对应Type |
---|---|
.gif | image/gif |
.html | text/html |
.jpeg | image/jpeg |
.jpg | image/jpeg或application/X-jpg |
.mp4 | video/mpeg4 |
.mpeg | video/mpg |
.mp3 | audio/mp3 |
.js | text/javascript或application/X- javascript |
.css | text/css |
//引入http模块
const http= require("http");
//创建服务,返回一个服务对象,监听对象动态
const server = http.createServer();
//监听request事件,只有客户端有请求,就会被监听到
server.on("request",(request,response)=>{
console.log(request)//接收前台请求的对象
console.log(response)//返回给前台数据的对象
console.log(request.url) //请求路径
console.log(request.method)//请求方式
console.log(request.httpVersion)//http协议版本
response.write("hello nodejs")
//在res.end调用之前可以多次调用
//设置请求头,两者方式一样
response.setHeader("Content-type","Application/json")
response.writeHead(200,{'Content-type':'image/png'})
response.end("end")//传输数据 结束响应,告知客户端所有发送已经结束,必须要有这个
})
//监听这个服务
server.listen(3000,()=>{
console.log("服务器启动成功")
})
fs内置模块
是node.js提供的文件操作API. 用于对系统文件及目录进行读写操作,是一个非常重要的模块,对文件的操作都基于他.该模块的所有方法都有同步和异步两种方式,下面是FS内置模块的一些常用方法.
1:读取文件===>readFile(异步读取)和readFIleSync(同步读取)
var http = require("http");
var fs = require("fs");
http.createServer(function (request, response) {
response.setHeader('Content-Type', 'text/plain; charset=utf-8');
//乱码问题
fs.readFile('./a.text', 'utf8', function (err, data) {
if (err) {
response.write('404');//找不到文件返回404
} else {
response.write(data);//找到文件返回文件信息
}
response.end();
})
}).listen(3000)
console.log('Server running at http://127.0.0.1:3000/');
页面上会显示 a.text文件里面的内容
2:读取之后再写入文件
fs.readFile('./a.text', function (err, data) {
if (err) {
} else {
fs.writeFile("./b.text", data, err => {
})
}
})
3:追加文件
// 追加文件
//data是你需要追加的内容
fs.appendFile("./b.text", data, "utf8", err => {
if (err) {
console.log(err);
} else {
console.log("追加成功!");
}
})
4:创建目录
// 创建目录
fs.mkdir("./mkdir/", (err) => {
if (err) {
console.log(err);
} else {
console.log("创建目录成功!");
}
})
5:读取文件夹
fs.readdir("./mkdir", (data, err) => {
console.log(err);
console.log(data);//返回一个包含文件名和文件夹名称的数组 [ 'a.html', 'b.html', 'c.html', 'd.html' ]
})
6:删除文件夹
fs.rmdir("./mkdir", { recursive: true }, (err) => {
if (err) {
console.log(err);
} else {
console.log("删除成功!");
}
})
内置模块 path
1:path.basename()返回路径的最后一部分
常用于用来获取文件名或者URL中带的参数
// 内置模块path
var path = require("path")
var psth1 = "http://www.baidu.com.cn/img.jpg"
var imgName = path.basename(psth1); //返回路径的最后一部分,个人认为用这个来获取文件名或者URL中带的参数
console.log(imgName);
//打印出 img.jpg
2:path.jion() 路径合并
var paths = path.join('web1804', 'html', 'css');
console.log(paths); //web1804\html\css
3:path.parse()返回路径字符串的对象
var path1 = 'http://www.baidu.com.cn/img.jpg';
var url = path.parse(path1);
console.log(url);
// 输出结果
// {
// root: '',
// dir: 'http://www.baidu.com.cn',
// base: 'img.jpg',
// ext: '.jpg',
// name: 'img'
// }
URL模块
URL的格式构成:
协议+认证+主机+端口+路径+查询字符串+哈希值。
1:url.parse() 对一段地址进行解析 只传一个参数
没有设置第二个参数为true时,query属性为一个字符串类型
var url = require('url')
var urlobj = url.parse("http://user:pass@host.com:8080/p/a/t/h?query=string#hash");
console.log(urlobj);
//输出结果
urlobj = {
protocol: 'http:',
slashes: true,
auth: 'user:pass',
host: 'host.com:8080',
port: '8080',
hostname: 'host.com',
hash: '#hash',
search: '?query=string',
query: 'query=string',
pathname: '/p/a/t/h',
path: '/p/a/t/h?query=string',
href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
}
2:url.parse() 对一段地址进行解析 传两个参数
负责把一个url地址转换成一个url的json对象。
url.parse("http://user:pass@host.com:8080/p/a/t/h?query=string#hash",true);
/*
返回值:
{
protocol: 'http:',
slashes: true,
auth: 'user:pass',
host: 'host.com:8080',
port: '8080',
hostname: 'host.com',
hash: '#hash',
search: '?query=string',
query: { query: 'string' },
pathname: '/p/a/t/h',
path: '/p/a/t/h?query=string',
href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
}
*/
3:url.format(urlObj)
负责把一个url的json对象转换成合法的url地址。
参数:urlObj指一个url对象
var urlobj = url.format({
protocol: "http:",
host: "182.163.0:60",
port: "60"
});
console.log(urlobj);
// 输出结果 :http://182.163.0:60