Node - HTTP服务
http
使用Node,开发者能够快速的构建一个web服务器
一个简单的访问http路径的实例
\\1.引入http服务
const http = require('http');
\\新建一个serve
let serve = http.createServer();
serve.on('request',function (){
console.log('请求发送成功了');
});
serve.listen(4000,function (){
console.log('服务创建成功,访问http:\\127.0.0.1:4000\');
});
复制代码
http进行通信
当被访问3000端口时,通过response.write('text'),response.end()响应想问消息; 在Node.js中,我们向客户端写入字符,需要注意字符集格式,不然很容易出现乱码。 所以在写入时,需要使用response的writeHead方法设置字符集 如HTML:response.writeHead(200, {'Content-Type': 'text\html;charset=UTF-8'},不同格式需要换成不同的字符集。
\\1.引入http服务
const http = require('http');
\\2.创建一个服务
let serve = http.createServer();
serve.on('request',function (request,response){
console.log('请求数据'+request.url);
\\响应数据
response.write('http');
response.write('response:hi');
response.end();
});
\\绑定服务器
serve.listen(3000,function (){
console.log('服务器访问成功,访问:http:\\127.0.0.1:3000');
});
复制代码
也可以这么写
const http = require('http')
http.createServer(function (request,response){
response.writeHead(200, {'Content-Type': 'text\html;charset=UTF-8'})
response.write('Hello Node.js Serve runing....')
response.end()
}).listen(3000)
复制代码
URL模块
url.parse方法
request.url可以获取请求的地址后缀,比如我们,需要注意的是request.url会请求两次,第一次是请求连接,第二次会请求favcion.ico,可以通过url.parse看出
const http = require('http')
const url = require('url');
http.createServer(function (request,response){
\*
* url路径去除favicon.ico情况
*\
let result = url.parse(request.url,true)
console.log(result)
\* response.writeHead(200, {'Content-Type': 'text\html;charset=UTF-8'})
response.write('Hello Node.js Serve runing....')
response.end() *\
}).listen(3000)
复制代码
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?name=wangly&password=123456',
query:
[Object: null prototype] { name: 'wangly', password: '123456' },
pathname: '\',
path: '\?name=wangly&password=123456',
href: '\?name=wangly&password=123456' }
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: [Object: null prototype] {},
pathname: '\favicon.ico',
path: '\favicon.ico',
href: '\favicon.ico' }
复制代码
因此我们需要判断,使得避免进入favicon.ico路径。 所以。我们需要使用:
if(request.url != "\favicon.ico"){
let result = url.parse(request.url,true)
console.log(result)
}
\\这个时候就只返回用户请求的url
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?name=wangly&password=123456',
query:
[Object: null prototype] { name: 'wangly', password: '123456' },
pathname: '\',
path: '\?name=wangly&password=123456',
href: '\?name=wangly&password=123456' }
复制代码
url.parse(URL,Boolean)可以传递两个参数,第一个是一个URL字符串,第二个是Boolean值,当为true的时候会将get提交的结果以对象的形式返回给开发者。 ture = query:[Object: null prototype] { name: 'wangly', password: '123456' } false = query: 'name=wangly&password=123456',
url.format方法
format方法则是将Url对象反之转换成为url连接,如果说parse是来的话,那么format就是回的意思。 我们可以来实验它
const http = require('http')
const url = require('url')
http.createServer(function (request,response){
\*
* url路径去除favicon.ico情况
*\
if(request.url !== "\favicon.ico"){
let result = url.parse(request.url,true)
console.log('URL的parse方法')
console.log(result)
console.log('URL的format方法')
console.log(url.format(result))
}
}).listen(3000)
复制代码
输出console
C:\Users\Wangly\Desktop\node\http>node serve.js
URL的parse方法
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?name=wangly&password=123456',
query:
[Object: null prototype] { name: 'wangly', password: '123456' },
pathname: '\user',
path: '\user?name=wangly&password=123456',
href: '\user?name=wangly&password=123456' }
URL的format方法
\user?name=wangly&password=123456
复制代码
url.resolve方法
主要是拼接字符串,用作改变 在request.url下追加一个路径地址
const http = require('http')
const url = require('url')
http.createServer(function (request,response){
\*
* url路径去除favicon.ico情况
*\
if(request.url !== "\favicon.ico"){
let result = url.resolve(request.url,'我是拼接的字符串')
console.log(result);
}
}).listen(3000)
复制代码
不出意外输出应该是+我们需要的字符串
C:\Users\Wangly\Desktop\node\http>node serve.js
\我是拼接的字符串
复制代码