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

本文介绍如何使用Node.js快速构建HTTP服务,包括服务创建、响应处理及URL解析等关键操作,帮助开发者掌握基本的Web服务器搭建技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 
\我是拼接的字符串

复制代码

转载于:https://juejin.im/post/5cb44e245188251b027ede91

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值