【英文】Nodejs内置模块

Preface

Node.js built-in module study notes

fs File System Module

  • When using relative paths, you may encounter dynamic concatenation errors.
    • Principle: When using relative paths, the current path is concatenated with the relative path to form an absolute path, and then the file is read or written through the absolute path.
    • Dynamic concatenation error: If the current path where you are located is not in the same directory as the path where the code is located, the dynamically concatenated absolute path will be incorrect.

Import Module

1
const fs = require("fs");

Read File Content

  • If the file is read successfully, err is null and dataStr is the content of the file.
  • If the file reading fails, err is the error object, and dataStr is undefined.

<file>: File path to read
utf-8: Specify character encoding

1
2
3
4
5
6
fs.readFile("<file>", "utf-8", function (err, dataStr) {
if (err) {
return console.log(err.message);
}
console.log(dataStr);
});

Write File Content

  • If the file name to write already exists, the original file will be overwritten.
  • When writing a file, you can only create a file, not a directory. If the directory does not exist, an error will be reported.

<file>: File path to write
<text>: Content to write
utf-8: Specify character encoding. It can be omitted, and the default value is utf-8.

1
2
3
4
5
fs.writeFile("<file>", "<text>", "utf-8", function (err) {
if (err) {
return console.log(err.message);
}
});

path Path Module

Import Module

1
const path = require("path");

Concatenate Multiple Paths

  • Although + can also be used to concatenate paths, it cannot automatically optimize ./ and ../ in relative paths.

__dirname: The directory where the current js file is located.

1
path.join(__dirname, "./", "../", ...);

Resolve the Filename (including the extension) in the Path

<path>: File path

1
path.basename("<path>");

Resolve the Filename (excluding the extension) in the Path

.<extname>: File extension

1
path.basename("<path>", ".<extname>");

Resolve the File Extension in the Path

1
path.extname("<path>");

http Module

Create Web Server

80: Specify the port number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Import module
const http = require("http");
// Create web server instance
const server = http.createServer();
// Bind event
server.on("request", function(req, resp) {
// Receive request and return response
...
});
// Bind port and start server
server.listen(80, function() {
// Code executed after the server starts successfully
...
});

Request Object

Get the Requested Resource Path

1
req.url;

Get the Request Method

1
req.method;

Response Object

Return Response

  • Send response data to the client and end the current request.

<str>: Data to be responded to the client

1
resp.end(<str>);

Set Response Headers

<key>: Response header key
<value>: Response header value

1
resp.setHeader("<key>", "<value>");
Solve the Chinese garbled characters in the response data
1
resp.setHeader("Content-Type", "text/html; charset=utf-8");

Done

References

Bilibili - Heima Programmer