Home > Web Front-end > JS Tutorial > body text

Node.js Basics - Essential Things to Know

王林
Release: 2024-09-12 10:33:07
Original
492 people have browsed it

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 
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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}`);
});
Copy after login

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');
});
Copy after login

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
Copy after login

The above is the detailed content of Node.js Basics - Essential Things to Know. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!