Node JS中的路径模块

Node is essentially a runtime environment for JavaScript programming language.

Node本质上是JavaScript编程语言的运行时环境。

In C, C++ we have header files; In Java, we have inbuilt classes; Similarly, in Node JS we have inbuilt modules.

C中C ++有头文件。 在Java中 ,我们有内置的类。 同样,在Node JS中,我们具有内置模块。

These modules are ready to be used by developers and come with a lot of objects and methods that can get complex things done in one command. Hence decreasing coder’s effort by a large amount.

这些模块可供开发人员使用,并带有许多对象和方法,可以通过一个命令完成复杂的工作。 因此,大大减少了编码人员的工作量。

In this article, we are going to discuss one of the inbuilt modules i.e. PATH MODULE. It provides utilities for working with file and directory paths.

在本文中,我们将讨论内置模块之一,即PATH MODULE 。 它提供了用于处理文件和目录路径的实用程序。

Here, I have a sample code for understanding basics of Path module.

在这里,我有一个示例代码,用于了解Path模块的基础

Code:

码:

var path=require('path');

console.log(__dirname);
console.log(__filename);

var way=__filename;

console.log(path.normalize(way));
console.log(path.extname(way));
console.log(path.dirname(way));
console.log(path.basename(way));

Output

输出量

Node JS | Path Module Output

Explanation of code

代码说明

For using any inbuilt module we first need to include it in our source code. This is done in line number 1.

为了使用任何内置模块,我们首先需要将其包含在源代码中。 这是在第1行中完成的。

require is a keyword and path is the name of the module to be included, it is stored in the variable path (var path). One can even use const instead of var. Also, we could use any other name (on the left side of =) instead of the path but it is a universal convention to use the same name as of module name.

require是一个关键字,而path是要包含的模块的名称,它存储在变量path ( var path )中。 甚至可以使用const而不是var 。 另外,我们可以使用其他任何名称(在=的左侧)代替路径,但是使用与模块名称相同的名称是一种通用约定。

In the last 4 lines you can see different methods of path object being used.

在最后4行中,您可以看到正在使用的路径对象的不同方法。

  • EXTNAME - returns extension of the base file.

    EXTNAME-返回基础文件的扩展名。

  • BASENAME - returns name of the base file.

    BASENAME-返回基础文件的名称。

  • DIRNAME - returns whole path of directory or folder in which the base file is present.

    DIRNAME-返回基本文件所在的目录或文件夹的完整路径。

翻译自: https://www.includehelp.com/node-js/path-module.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值