Node basic operation

Node first experience

  • Command line opening method:
    1.win+r-type cmd and press Enter
    2. In any folder, hold down shift, right-click, and click here to open the command line window
    3. Type cmd in the folder path , Press Enter
    4. In the start menu, enter cmd-run as administrator
  • Exit the node method:
    1. Enter on the command line: .exit
    2. Or press ctrl+c twice

Run js code on the command line

  • Open the command line as an administrator and write the command:node
  • Press enter directly, you will see the cursor blinking, and we enter the node code writing environment
  • Just write the code directly
    Insert picture description here
  • The running result is the same as the result in the browser console

Run the js file on the command line

  • First create a js file, my file path is桌面/demo文件夹/idnex.js
  • Write some js code in the file
// index.js
console.log('hello node')
  • Open the command line, make the path of the command line consistent with the directory where you store the js file to be executed
    Insert picture description here
  • After switching, we directly use the command to run the js file we prepared:node index.js
  • Then it will run the js file we just wrote on the command line, and it will be output in the console hello node
    Insert picture description here

Linux operation

  • What is LINUX operation
  • In fact, we use commands on the command line to operate our computer
  • Because our node is to run js on the command line
  • So we have to understand some commonly used command line commands

Directory operations

Directory operation is to manipulate our command line path

  • Switch drive letter:盘符:
  • Enter a directory under the current directory:cd 文件夹名称
  • Return to the previous directory:cd ..
  • Return to the previous directory:cd ../..
  • View all files in the current directory:dir
  • Display all files in the current directory and all files in subdirectories in a tree structure:tree

File operations

File operation is to create files or folders through instructions

  • Create a folder:md 文件夹名称
  • Delete folder:rd 文件夹名称
  • Copy folder:xcopy 被复制的文件名称 新的文件名称
  • Create a file:type nul > 文件夹名称
  • Copy files:copy 被复制的文件名称 新的文件名称
  • Move files:move 旧的文件夹名称 新的文件路径
  • Delete Files:del 文件名称
  • Rename the file:ren 旧的文件名称 新的文件名称
  • View the text content in the file:type 文件名称
  • Write content to the text:echo 要写入的文本内容 > 目标文件名

Other instructions

  • Clear screen:cls
  • View current computer IP information:ipconfig
  • Test the speed of a certain link address:ping 链接地址
  • View computer information:systeminfo

Import and Export

In actual development, generally everyone completes a function, and then combines multiple functions to complete the entire project. We call this process modular development. Each file is a separate module.
The function developed by each person is a single file. To combine multiple files, you need to export these multiple files and then import them in a final project file.

Import

  • In the node which we use requireto import a file
// 我是 index.js 文件
require('./a.js')

console.log('我是 index.js 文件')
  • When I run from the command line index.jswhen the file
  • First will a.jsfile to run again
  • Then continue to execute the code inside my own file
  • You can also accept the content exported from another file when importing
// a 接受到的内容就是 a.js 这个文件导出的内容
// 如果 a.js 文件中什么都没有导出,那么接受到的就是一个 空对象
const a = require('./a.js')

Export

Every file has an object: module. There is an attribute in this object called exports, and the value is also an object, which is an empty object by default. The current file exports this exports object by default, and the exported content is written in this object.

// 我是 a.js

module.exports.name = 'Jack'
module.exports.age = 18
  • When this file is imported in the future, the received content is an object with two members
// 我是 index.js

const a = require('./a.js')

console.log(a) // { name: 'Jack', age: 18 }

Guess you like

Origin blog.csdn.net/qq_45677671/article/details/114537593