当前位置:网站首页>Nodejs (02) - built in module
Nodejs (02) - built in module
2022-07-02 05:10:00 【Bump】
- os Operating system modules
- path Path module
- url modular
- fs File module
- Simulate server
This article only aims at window operating system
This article only records a few commonly used attributes 、 Some modules , Not all practice
details API Please refer to the document :http://nodejs.cn/api/os.html
os Operating system modules
grammar
var os=require("os");
Method
| Method name | Detailed explanation |
|---|---|
| EOL | Line break |
| hostname() | Host name |
| type() | System |
| totalmem() | Memory |
| cpus() | be-all cpu |
Example
var os=require("os");
console.log('hello'+os.EOL+"nodejs");// Line break
console.log(' Host name '+os.hostname());
console.log(" System "+os.type());
console.log(" Memory :"+os.totalmem());
console.log(os.cpus());

path Path module
grammar
var path=require("path");
Method
| Method name | Detailed explanation |
|---|---|
| basename( File path ) | file name |
| dirname( File path ) | Folder name |
| extname( File path ) | File suffix |
| format( object ) | Splicing path for example :dir: Folder path base: file name |
| parse( File path ) | Split path ( And format contrary ) |
Example
/* Path module */
var path=require('path');
var testfile='D:\\pk\\dconfig.json';
var name=path.basename(testfile);// file name
var dname=path.dirname(testfile);// Folder name
var extname=path.extname(testfile);// Suffix name
let ok=path.format({
dir:'c:\\images\\users',
base:'bg1.jpg'
});// Splicing path
let parse=path.parse('c:\\images\\users\\bg1.jpg');// And format contrary
console.log(" file name "+name+" Folder name :"+dname+" suffix :"+extname);
console.log(" Splicing path :"+ok);
console.log(" Split path :",parse);

url modular
grammar
const url=new URL('https://example.org/abc?123#find');
attribute
| Property name | Detailed explanation |
|---|---|
| href | URL path |
| host | Website host part |
| pathname | Parameter name |
| search | Parameter values |
| hash | Set get fragment part # |
Example
/** * url modular */
const url=new URL('https://example.org/abc?123#find');
console.log(url.hash);// Get the segment
// Set the segment
url.hash="sethash";
console.log(url.href);// The modified website
console.log(url.host);// Website host part
console.log(url.pathname);
console.log(url.search);

fs File read and write
grammar
var fs=require('fs');
Reading documents
Create a data.txt The contents of the document :
hello filesystem
CSDN---> Bump
// read
fs.readFile('day1/data.txt',(err,data)=>{
if(err){
console.log(" Read failed ");
return;
}
//data Is the data read
// console.log(data);// binary data
console.log(data.toString());
})

Writing documents
const data='Hello Bump !';
fs.writeFile('day1/pengke.txt',data,(error)=>{
if(error){
console.log(" Failed to write file ");
return;
}
console.log(" Write successfully !!!");
})
Finally get a file pengke.txt
Hello Bump !
Simulate server (http)
Key modules :http
Example
/** * Simulate server */
var http=require("http");
var port=1234;// Port number
var server=http.createServer((req,resp)=>{
// Get browser request
console.log(" Get request :",req.url);
if(req.url=='/'){
$msg=' This is the home page ';
}else if(req.url=='/login'){
$msg=' Here is the landing page ';
}else{
$msg='404';
}
// The response data
resp.writeHead(200,{
'Content-Type':'text/html;charset=utf-8'});
resp.write($msg);
resp.end();
});
server.listen(port);
console.log(" The server is running ....http://127.0.0.1:"+port);



practice (fs+http)
By modifying the http Path to write content to txt And get it and display it on the page
/** * Simulate server */
var http=require("http");
const fs=require("fs");
var port=1234;// Port number
var server=http.createServer((req,resp)=>{
var data=' This is the home page ';
// Get browser request
console.log(" Get request :",req.url);
if(req.url=='/'){
console.log("index");
data=' This is the home page ';
}else if(req.url=='/login'){
console.log("login");
data=' Here is the landing page ';
}else{
console.log("404");
data='404';
}
fs.writeFile('day1/pengke.txt',data,(error)=>{
console.log("data="+data);
if(error){
console.log(" Failed to write file ");
return;
}
console.log(" Write successfully !!!");
})
fs.readFile('day1/pengke.txt','utf-8',(err,datas)=>{
if(err){
console.log(" Read failed ");
return;
}
// The response data
resp.writeHead(200,{
'Content-Type':'text/html;charset=utf-8'});
resp.write(datas);
// resp.end();
})
});
server.listen(port);
console.log(" The server is running ....http://127.0.0.1:"+port);
The practice of the built-in module is over .
边栏推荐
- Future trend of automated testing ----- self healing technology
- Here comes the chicken soup! Keep this quick guide for data analysts
- Mysql重点难题(2)汇总
- Set the default style of scroll bar Google browser
- 7.1模拟赛总结
- No logic is executed after the El form is validated successfully
- Fabric.js 自由绘制矩形
- 7.1 Résumé du concours de simulation
- 函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
- 数学问题(数论)试除法做质数的判断、分解质因数,筛质数
猜你喜欢

How to configure PostgreSQL 12.9 to allow remote connections
![[common error] the DDR type of FPGA device is selected incorrectly](/img/f3/be66bcfafeed581add6d48654dfe34.jpg)
[common error] the DDR type of FPGA device is selected incorrectly

运维工作的“本手、妙手、俗手”

Solution: the agent throws an exception error

数学问题(数论)试除法做质数的判断、分解质因数,筛质数

Cubemx DMA notes

Steam教育的实际问题解决能力

Simple and practical accounting software, so that accounts can be checked

Fabric.js IText 上标和下标
![[high speed bus] Introduction to jesd204b](/img/78/1a0a3672e63058da6d98da95aa3cf2.jpg)
[high speed bus] Introduction to jesd204b
随机推荐
Feign realizes file uploading and downloading
Differential identities (help find mean, variance, and other moments)
The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
[Yu Yue education] autumn 2021 reference materials of Tongji University
Summary of MySQL key challenges (2)
LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
[bus interface] Axi interface
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Fabric.js 基础笔刷
Feign realizes file uploading and downloading
Lay the foundation for children's programming to become a basic discipline
Leetcode 18 problem [sum of four numbers] recursive solution
Basic differences between Oracle and MySQL (entry level)
Fabric.js 自由绘制矩形
农业生态领域智能机器人的应用
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
Paddlepaddle project source code
Video cover image setting, put cover images into multiple videos in the simplest way
Steam教育的实际问题解决能力