English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS 基础教程

NodeJS Express.js

NodeJS 缓冲&URL;

NodeJS MySql

NodeJS MongoDB

NodeJS 文件(FS)

NodeJS 其他

Analyse d'URL avec Node.js

Node.js解析URL:在本教程中,我们将学习如何在Node.js中解析URL或将URL拆分为可读部分,并使用内置的Node.js URL模块提取搜索参数。

要在Node.js中解析URL:使用url模块,并在解析和查询功能的帮助下,可以提取URL的所有组件。

Node.js解析URL组件–分步指南

以下是有关如何将URL解析为Node.js中可读部分的程序的逐步指南。

  • Étape1:包括网址模块

    var url = require(‘url‘);
  • Étape2步:将URL带到变量中以下是我们将分析的示例URL。

    var address = ‘http://localhost:8080/index.php?type=page&action=update&id=5221‘;
  • Étape3:使用解析功能解析网址。

    var q = url.parse(address,true);
  • Étape4: Utilisez l'opérateur de point pour extraire les chaînes HOST, PATHNAME et SEARCH.

    q.host q.pathname q.search
  • Étape5: Utilisez la fonction de requête pour analyser les paramètres de recherche de l'URL.

    var qdata = q.query;
  • Étape6Étape : Accéder à la recherche

    qdata.type qdata.action qdata.id

Programme complet Node.js qui peut analyser l'URL en parties lisibles dans Node.js

 
// Contient le module d'URL
var url = require('url'); 
var address = 'http://localhost:8080/index.php?type=page&action=update&id=5221'; 
var q = url.parse(address, true); 
 
console.log(q.host); //Retour 'localhost :'8080
console.log(q.pathname); //Retour/index.php'
console.log(q.search); //retourne '?type=page&action=update&id=5221'
 
var qdata = q.query; // Retourne un objet : {Type : page, Operation : 'mise à jour', id = '5221}
console.log(qdata.type); //Retour "page"
console.log(qdata.action); //Retour "mise à jour"
console.log(qdata.id); //Retour " 5221"

Sortie du terminal

$ node urlParsingExample.js 
localhost:8080
/index.php
 ?type=page&action=update&id=5221
page
mise à jour
5221

Résumé :

Dans ce tutoriel Node.js –Analyser l'URLNous avons appris à utiliser le module intégré URL de Node.js pour解析 ou décomposer l'URL en parties lisibles dans Node.js. Et extraire l'hôte, le nom de chemin, la recherche et les paramètres de recherche.