Why Node.js is becoming the new favorite of backend developers

gitter4coding - Oct 21 - - Dev Community

Image description

1. Introduction to Node.js

Node.js is an open-source, cross-platform JavaScript runtime environment built on the Chrome V8 engine, allowing developers to run JavaScript code on the server side. The V8 engine is a technology developed by Google to enhance the execution speed of JavaScript. With Node.js, developers can handle various requests initiated by users in the browser on the server.

2. Installing and Configuring Node.js

2.1 Download and Installation

Visit Servbay to download and install Node.js. Follow the prompts for installation.

2.2 Configuration

  1. Configure according to the Servbay official tutorial
  2. Set up the location for npm dependencies

3. Relationship between Node.js and npm

npm (Node Package Manager) is a package management tool that is installed along with Node.js. When developing Node.js applications, developers can conveniently install and manage various functionality modules via npm. For example, to install axios:

npm install axios
Enter fullscreen mode Exit fullscreen mode

npm repository address: npmjs.com

4. Built-in Modules

Learning Node.js primarily involves mastering its built-in APIs and commonly used third-party APIs. Here are some commonly used built-in modules:

4.1 File Operations: fs

The fs module is used for file operations.

Reading a File

const fs = require('fs');
fs.readFile('./zhifou.txt', 'utf8', function (err, data) {
if (err) {
return console.log('Read failed: ' + err.message);
}
console.log(data);
});
Enter fullscreen mode Exit fullscreen mode




Writing a File


const fs = require('fs');
fs.writeFile('./hello.txt', 'The sun is high in the sky, sweat drips onto the soil', function (err) {
if (err) {
return console.log('Write failed: ' + err.message);
}
console.log('Write successful!');
});
Enter fullscreen mode Exit fullscreen mode




4.2 Path Operations: path

The path module is used for handling file paths.

Join

const path = require('path');
let finalPath = path.join('/a', '/b', '/c');
console.log(finalPath);
Enter fullscreen mode Exit fullscreen mode




Basename


const path = require('path');
const fpath = '/a/b/zhifou.js';
console.log("name1:", path.basename(fpath));
console.log("name2:", path.basename(fpath, '.js'));
Enter fullscreen mode Exit fullscreen mode




Extname


const path = require('path');
console.log("name:", path.extname('/a/b/zhifou.js'));
Enter fullscreen mode Exit fullscreen mode




4.3 HTTP

The http module is used to create a web server.

const http = require('http');
const server = http.createServer();
server.on('request', (req, res) => {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.end(Requested URL: ${req.url}, Request Method: ${req.method});
});
server.listen(8081, () => {
console.log('Server running at http://127.0.0.1:8081');
});
Enter fullscreen mode Exit fullscreen mode




5. Modularization

5.1 Node.js Modularization

Node.js follows the CommonJS module specification, where each file is a module. Modules are loaded using require, and module content is exposed using exports and module.exports.

5.2 ES6 Modularization

ES6 modularization integrates better with front-end technology stacks (such as React and Vue). It uses import and export keywords for module importing and exporting.

Export Methods

  • Default Export:

    export default { name: "Zhifou", age: 23, desc: function() { console.log("WeChat Official Account: Zhifou Technology"); } };

  • Named Export:

    export { name, age, desc };

Import Methods

  • Default Import:

    import common from './common.js';

  • Destructuring Import:

    import { name, age } from './common.js';

6. Common Third-Party APIs

Common third-party APIs include axios, pinia, vuex, mysql, express, etc. Learn them based on your needs.

7. Common Node.js Questions

  1. What is npm? npm is a package manager that facilitates the installation and management of dependency packages for developers.
  2. What is the relationship between Node.js and Vue? Vue does not depend on Node.js, but tools like Vue CLI and build tools like Webpack are developed based on Node.js.
  3. What is the relationship between Vite and Node.js? Vite is a front-end build tool that requires a Node.js environment to run.
  4. What is the relationship between Node.js and npm? npm is the package manager for Node.js, used to manage and install dependency packages.
  5. What is the essence of npm run dev? Executing npm run dev starts a development server, providing services and automatically listening for file changes to enhance development efficiency.
. . . . . . . . . . .