Rumah  >  Artikel  >  hujung hadapan web  >  Asas Node.js - Perkara Penting untuk Tahu

Asas Node.js - Perkara Penting untuk Tahu

王林
王林asal
2024-09-12 10:33:07582semak imbas

Node.js Basics - Essential Things to Know

Concept Highlights:

  1. Node.js Globals
  2. Node.js Modules
  3. The path Module
  4. The process Object
  5. Handling Input and Output
  6. File Management and Streams

1. Node.js Globals

Node.js comes with several global objects and functions that are available anywhere in an application without needing to require() them. Some of the key global objects include:

  • __dirname: The directory name of the current module.
  • __filename: The full path of the current file.
  • setTimeout(), clearTimeout(), setInterval(), clearInterval(): Functions for managing asynchronous timing.

e.g.)

console.log(__dirname); // outputs the current directory
console.log(__filename); // outputs the full path of the current file 

2. Node.js Modules

Node.js follows a modular structure, where code is divided into smaller, reusable modules. You can load built-in or custom modules using the require() function.

e.g.) There are three types of modules in Node.js:

  1. Core Modules: provided by Node.js like fs, http, and path.
  2. Third-Party Modules: installed via npm such as express or lodash.
  3. Custom Modules: created by you to organize your code.
const fs = require('fs'); // Require the built-in file system module

3. The path Module

The path module in Node.js provides utilities for working with file and directory paths. It's especially useful for making your code platform-independent since path separators (\ on Windows) can vary between operating systems.

e.g.) Key methods in the path module:

  • path.join() joins multiple path segments into one.
  • path.basename() returns the last part of a path (usually the file name).
  • paht.extname() returns the file extension.
const path = require('path');

const filePath = path.join(__dirname, 'folder', 'file.txt');
console.log(filePath); // Combines the paths to create a full file path

4. The process Object

The process object in Node.js provides information about and control over the current Node.js process. It is a global object that allows you to internet with the runtime environment.

e.g.) Some useful properties and methods of process include:

  • process.argv: arguments passed to the Node.js process.
  • process.env: environment variables.
  • process.exit(): terminates the process.
console.log(process.argv); // Returns an array of command-line arguments
console.log(process.env); // Accesses environment variables

5. Handling Input and Output

Node.js provides simple ways to handle input and output, particularly through its process object for working with standard input and output.

e.g.) This example listens for user input and logs it to the console. For more advanced I/O handling, you can also use streams, which allow you to process data piece by piece instead of loading the entire I/O into memory at once.

process.stdin.on('data', (data) => {
  console.log(`You typed: ${data}`);
});

6. File Management and Streams

File management is a critical part of many Node.js applications, and Node's fs (file system) module provides a range of methods to work with the file system. You can read, write, and manage files using the asynchronous or synchronous APIs.

e.g.)

const fs = require('fs');

// Asynchronous file reading
fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

// Writing to a file
fs.writeFile('output.txt', 'This is some content', (err) => {
  if (err) throw err;
  console.log('File written successfully');
});

Node.js also has a powerful system for working with streams, which are used to handle large amounts of data efficiently. Streams are often used for reading/writing files or handling network communication.

const fs = require('fs');

const readStream = fs.createReadStream('example.txt');
const writeStream = fs.createWriteStream('output.txt');

readStream.pipe(writeStream); // Piping data from one file to another

Atas ialah kandungan terperinci Asas Node.js - Perkara Penting untuk Tahu. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn