Node.js学习之道-http+url基础

Node - HTTP服务

http

使用Node,开发者可以快速的构建一个web服务器html

一个简单的访问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'},不一样格式须要换成不一样的字符集。node

\\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看出web

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路径。 因此。咱们须要使用:bash

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就是回的意思。 咱们能够来实验它ui

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)
复制代码

输出consoleurl

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下追加一个路径地址spa

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)
复制代码

不出意外输出应该是+咱们须要的字符串prototype

C:\Users\Wangly\Desktop\node\http>node serve.js 
\我是拼接的字符串

复制代码
相关文章
相关标签/搜索