Node.js fs 모듈이란 무엇입니까?

7241 단어 javascriptnode
내장된 Node.js 파일 시스템 모듈을 사용하면 운영 체제의 파일 시스템과 상호 작용할 수 있습니다. 모든 Node.js 프로젝트에서 사용할 수 있으며 설치할 필요가 없습니다. 파일 시스템 모듈은 파일 읽기, 파일 쓰기, 파일 변경 감시 등을 위한 유용한 기능에 대한 액세스를 제공합니다. 전체 개요는 official documentation을 참조하십시오.

fs 모듈 사용

To use the file system module, require it. No need to install it, since it's part of the core module.

const fs = require('fs');

Commonly used features of the fs module to store, access, and manage data are:

  • fs.readFile to read data from a file,
  • fs.writeFile to write data to a file (also replaces the file if it already exists),
  • fs.watchFile to watch a file and get notified about changes, and
  • fs.appendFile to append data to a file.

상대 파일 경로

The majority of the fs methods take the file path as the first argument. You have to option an absolute or a relative path. Relative paths will begin with a single dot ( . ), which indicates from the current directory , or two dots ( .. ), which indicates from the parent directory .

When using a relative path (like ../file.md ) with the fs module the path will be resolved from the perspective of the current working directory where you run the script in or where the program was executed, not from the current file. This behaviour is different compared to require . This is prone to errors, and you have to pay close attention to how you specify paths.

To specify a path relative to the file it is being executed in the __dirname keyword is of specific help. The __ dirname keyword expands to the absolute path of the directory of the file it is used in. For example, if you use __dirname from a file in /folder/subfolder/file.js , the value would be /folder/subfolder . Relative file paths and __ dirname can also be combined to create a relative lookup.

To join the two paths (from __dirname and the relative path to the file) and create a full path, you can use a Node.js core module path . The method join from the path module allows us to create a cross-platform filepath.

const fs = require('fs');
const path = require('path');

fs.writeFile(
  path.join(__dirname, '/output.md'),
  'Hello World! I am relative to the directory this scripts inherits.',
);

비동기 || 동기

All I/O operations in Node.js are done async to avoid blocking the event loop. Hence, the methods provided by the fs module are, by default, async and callback-based. As many modules in the Node.js ecosystem, there is a synchronous version as well, for example readFileSync .

In general, the best practice is to use async code , when working with the filesystem or other heavy operations. In some rare cases it could be that you want to do something first and block the execution until this specific thing is done. Loading configuration data on application startup could be such a case, see code below.

const fs = require('fs');
const config = JSON.parse(fs.readFileSync('./configFile.json');

Besides, this rare use case, always use async code when accessing the file system.

약속과 파일 시스템

The fs/promises API provides asynchronous file system methods that return promises. It was added in 2018 in Node.js v.10.0.0 . Node.js 버전 10에서 promise 지원은 실험적입니다(콘솔 경고). Node.js 버전 11부터는 완전히 지원됩니다.

Promise 기반 파일 시스템 메서드는 동기 버전의 Promise가 없지만 콜백 기반 메서드와 동일하게 작동합니다. 기본적으로 콜백 대신 Promise가 반환됩니다. 아래 코드 스니펫을 참조하세요.

const fs = require('fs').promises;

const writePromise = fs.writeFile('./output.txt', 'Hello World!');

writePromise
  .then(() => console.log('success!'))
  .catch(err => console.error(err));


클라우드 환경의 파일 시스템(임시 파일 시스템)

There is not always a persistent filesystem in the current environment available. Locally it is easy to persist files and interact with them later again. In cloud environments this is not always possible. For example the filesystem in AWS Lambda functions is only persisted for a short period. With the cloud host Heroku this is the same case, because of an ephemeral filesystem for the virtual machines used. Ephemeral file system only persist files for a short time, on a restart of a virtual machine the files created at runtime are gone.

Don't store state or uploads as files on a ephemeral file system, because the data can't be persisted.

TL;DR

  • FS enables interactions with the file system
  • Consider using __dirname and the path module to create full paths
  • Use ASYNC methods to not block execution.
  • Use SYNC methods only in rare cases (for example: loading configs before execution)
  • You can use promises with the fs (since Node.js v.10)
  • Don't store state or uploads as files on a ephemeral file system.

Thanks for reading and if you have any questions , use the comment function or send me a message .

If you want to know more about Node , 이것들을 보세요 Node Tutorials .

참조(그리고 큰 감사):

Node.js , HeyNode

좋은 웹페이지 즐겨찾기