NODE

  • What is a node

    • Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.
    • Node.js is a JavaScript runtime environment based on Chrome V8 engine.
  • This is explained on the Node website

    • Node is written in javascript as a back end
    • In other words, javascript with Node is not only a front-end language, but also a back-end language
  • The front-end javascript

    • Three core

      • ECMAScript
      • DOM
      • BOM
    • Operating content

      • The browser
      • Resolve compatibility issues
  • Back-end javascript (Node)

    • The core

      • ECMAScript
    • Operating content

      • The back-end code
      • The database
  • In other words, node doesn’t need to solve compatibility issues, don’t need DOM and BOM, just focus on business logic

Download the Node installation package

  • Our computer does not have a built-in Node environment

  • We need to manually download a Node installation package to install the Node environment

  • With the Node environment in place, we are ready to run Node

  • Download the way

    • Go directly to the Node website or Node Chinese website
    • Just click download

  • Note: When downloading from Node Chinese, select the installation package, not the binary file

    • Since binaries are a simple version, we need to configure our own environment variables to use them

Installing the Node Environment

  • After downloading, we directly download the good file double-click to run on the line
  • findNode - v10.16.2 - x64. MsiCorresponding file

  • At this point node is installed

Checking the Installation Environment

  • Check whether the installation is successful

  • Let’s open the run window (Win + R)

    • It’s the Windows key + R at the bottom of our keyboard
  • Write CMD and press Enter to go to our command line

  • Then write a command $node -v from the command line
  • Then press Enter to get a node version number
  • If the version number is displayed, the Node environment is successfully installed

  • At this point, we have our Node environment
  • We can then run our Node on the computer

At the beginning of the node experience

  • At this point, our Node environment is installed

  • Let’s try out Node

  • What exactly is our Node

    • It runs directly on the terminal (command line)jscode
    • You can also use.jsWrite a stack of filesjscode
    • And then you don’t need a browser, you just let us write itjsThe code runs on a terminal on our own computer

Write JS code directly in the terminal

  • Open command line
  • Written instructions$ node
  • Just press Enter to see the cursor flicker and enter the Node coding environment
  • Just write the code

Run a JS file on the command line

  • Let’s create a new folder

  • Write a JS file inside

    • I’m default hereDesktop/Demo folder /idnex.js
  • Write some JS code in the file

// index.js
console.log('hello node')
Copy the code
  • To open the command line, make sure that the command line path is the same as the directory where you store the js file to be executed

  • After the switch, we directly use the command to run the JS file we prepared$ node index.js
  • It will then run the js file we just wrote on the command line
  • It’s going to print it on the consolehello node

  • We now have a snippet of JS code running on the command line

  • That explains a little bit about what the website said at the beginning

    • Node.js is a JavaScript runtime environment based on Chrome V8 engine.

      • Once our Node is installed, we provide a Chrome V8 engine running environment from the command line
      • Run javascript code in this environment
      • This is Node.js

Common LINUX operations

  • What are LINUX operations
  • It’s basically at the command line using instructions to operate our computers
  • Because our Node is running JS on the command line
  • So let’s look at some common command-line commands

Directory operations

  • Directory operations are operations on our command line paths
  1. View all files in the current directory

    $ dir

  2. Displays all files in the current directory and all files in subdirectories in a tree structure

    $ tree

  3. Go to a directory in the current directory

    $CD Folder name

  4. Returns the upper directory

    $ cd ..

  5. Switch drive

    $d:

File operations

  • File operations are commands to create files or folders
  1. Creating a folder

    # creates a folder called test under the current directory

    $ md test

  2. Remove folders

    Remove the test folder from the current folder

    $ rd test

  3. Copy folder

    Copy the test folder named test2

    $ xcopy test test2

  4. Create a file

    # creates a file called index.js in the current directory

    $ type nul> index.js

  5. Make a copy of the document

    # to copy an index.js file named ceshi.js

    $ copy index.js ceshi.js

  6. Writes to text

    Write console.log(‘ Hello world’) to index.js

    $ echo console.log("hello world") > index.js

  7. View the text content in the file

    What is the text content in the index.js file

    $ type index.js

  8. Rename a file or directory

    # rename index.js to abc.js

    $ ren index.js abc.js

  9. Delete the file

    # delete index.js from the current directory

    $ del index.js

  10. Move files or folders

    # move the index.js file from the current directory to the a folder in the current directory

    $ move index.js a

Other instructions

  • I use it to do something else
  1. Clear the screen

    # clears the current screen

    $ cls

  2. View the IP address of the current PC

    # displays the IP address of the current computer

    $ ipconfig

  3. Tests the speed of a link address

    # indicates the speed of accessing Baidu’s website

    $ ping www.baidu.com

  4. Checking computer information

    # displays information about the current computer

    $ systeminfo

NODE import and export

  • nodeDevelopment is modular development
  • Each JS file is an independent module
  • Each has its own independent scope
  • We can merge multiple JS files together by importing and exporting them

The import

  • In Node, we userequireTo import a file
// I am the index.js file require('./a.js') console.log(' I am the index.js file ')Copy the code
  • When I run the index.js file from the command line

    • You will takea.jsRun the file again
    • Then I continue to execute the code inside my own file
  • You can also accept the content exported from another file when re-importing

Const a = require('./a.js') const a = require('./a.js')Copy the code

export

  • When we write a JS file, we can export some content
  • When this file is imported in the future, some content will be accepted
// I am A. js // Every JS file has an object called module // Inside module there is a member called module // every JS file exports module.exports by default. Module.exports. Name = 'Jack' module.exports. Age = 18Copy the code
  • When this file is imported in the future, it receives an object with two members
// 我是 index.js

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

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

modular

  • During node development

  • We make each function into a module independently

  • Then use import and export to correlate them together

    • To facilitate maintenance
    • Accurate positioning
  • We generally divide modules into three types

  1. Built-in modules (modules that come naturally with Node)
  2. Custom modules (files we wrote ourselves)
  3. Third party modules (modules written by others and downloaded from the Internet)

Common built-in modules of NODE

  • We just wrote our own module
  • Now let’s talk about common built-in modules

FS module

  • fsIs a built-in module of Node
  • It’s designed to manipulate files
  • When using directly import can be used
Const fs = require('fs') // We can use fs to manipulate filesCopy the code

Read file contents asynchronously

  • Asynchronously reads the contents of a file
Const fs = require('fs') // Fs.readfile ('./text.txt', 'utf8', function (err, data) {// err indicates the error./ / data indicates the error. Data has no content})Copy the code

Synchronously read file contents

  • Read the contents of a file synchronously
Const fs = fs.readfilesync ('./text.txt', 'utf8') // require('fs') // // If there is no error, res will get the contents of the fileCopy the code

Asynchronous file writing

  • Asynchronously write to a file
Const fs = require('fs') // There is usually no error when writing something to it // because if you don't have a file, you will create a file to write something to it // so the callback function is usually useless, Fs.writefile ('./text.txt', 'I want to write ', function () {console.log(' write done ')})Copy the code

Synchronous file writing

  • Write content to a file synchronously
Fs.writefilesync ('./text.txt', 'what I want to write ')Copy the code

The HTTP module

  • Because Node is a server-side language
  • So Node must also be able to start a server and start a service
  • httpThis module is designed to start the service, accept the request, and return the response
  • httpIs also a built-in module, directly imported to use the line
Const HTTP = require(' HTTP ') // Then we can use the HTTP module to enable the serviceCopy the code

Creating a service

  • To start, create a service
Const HTTP = require(' HTTP ') // create a service that listens on the HTTP protocol by default // this service listens on the localhost domain by default // Return value is this service const server = Http.createserver (function (request, response) {// Request contains all request information // response is all response information})Copy the code

Listen on a port

  • Determine which port the service listens on
// Create a service const server = http.createserver (function (request, Response) {// every request from the front end will trigger this function}) server.listen(8080, Function () {console.log('listening on port 8080')})Copy the code

Give a response

  • Give a simple response
// Create a service const server = http.createserver (function (request, Response.end ('hello world')}) server.listen(8080) {response.end('hello world')}) server.listen(8080, Function () {console.log('lintening on port 8080')}) function () {console.log('lintening on port 8080')})Copy the code
  • At this point, open your browser
  • Address bar inputlocalhost:8080
  • The browser responds with the texthello world

NPM

  • Before we installnodeThe environment will automatically help us install one togethernpmThe environment
  • Just like when we install some software, it automatically installs something for us, rightXXX Software Manager/XXX gameSomething like that
  • butnpmNot junk software, but a super useful tool for us

Checking installation

  • Same as checking node
  • Enter instructions at the command line$ npm -v
  • You can get a version number

Understand the NPM

  • What is NPM

  • We can think of it as a supermarket with all the plugins/libraries/frameworks we need

  • We need to download a jquery-Validation plugin

    • We can choose to download it from the official website
    • You can find it on GitHub and download it
    • Alternatively, you can download directly from the command line using NPM
  • We are going to download a bootstrap

    • We can choose to download it from the official website
    • You can find it on GitHub and download it
    • Alternatively, you can download directly from the command line using NPM
  • That is, NPM contains all of our third party stuff

  • When we need it, as long as we open the terminal, we can use the command to help us download

    • You don’t have to go to the official website anymore
  • Moreover, NPM can be used not only on the back end, but also on the front end

  • NPM is nothing more than a large package manager that depends on the Node environment

The use of NPM

  • We want to use NPM by simply opening the command line
  • As a package manager
  • Help us download some plug-in library frameworks and stuff for us to use

Download package

  • Open command line
  • Enter the download instructions
$NPM install jquery $NPM install jqueryCopy the code
  • Once the download is complete, there will be an extra folder in the current directory

    • callednode_modules
    • In this directory there will be a folder calledjquery
    • That’s what we need
  • NPM downloads the latest version of the package by default

  • We can also specify which version I want to download while downloading

$NPM install [email protected]Copy the code

Delete the package

  • To delete a package, go to the node_modules folder and delete the package folder

  • However, this is not a good idea, we should still use the command line instructions to delete packages

    # indicates that I want to delete the jquery package

    $ npm uninstall jquery

  • This way, the package will be uninstalled

Manage the project

  • Each of our projects may depend on many packages (plug-ins/libraries/frameworks)

  • NPM will help us keep track of the packages we are using for our current project

  • But only if you tell NPM, “You’re going to manage the whole folder for me.”

  • We still use instructions to tell NPM from the command line

    # tell NPM you will help us manage the entire folder (i.e. my entire project)

    $ npm init

NPM clears the cache

  • Sometimes, some packages fail in the middle of downloading for various reasons (e.g., suddenly no Internet connection).

  • The half-downloaded package is likely to be cached

  • The next time you download it, it will be a failure

  • So we need to clear the cache and then download again

    # to clear NPM cache

    $ npm cache clear -f

NRM

  • Our NPM works

  • But there is a drawback

    • Yeah, he’s helping us download stuff
    • But his download address is in a foreign country
    • That is, every time you use NPM to download, you go to a foreign server to download
    • There will be a lot of instability
    • And relatively long time
  • NRM is a tool used to switch the NPM download address (switch image source tool).

Install the NRM

  • If we want to use NRM, we need to install it ourselves

  • Because it is our tool, it can be installed using NPM

  • The installation is still done using instructions

  • The NRM is installed as a global dependency instead of an internal dependency

    • Global dependency, a computer installed once, always available
  • We use the directive to install a global NRM

    # indicates that a global NRM is installed

    $ npm install --global nrm

Inspection installation

  • After the installation is complete, we check to see if the installation is successful

  • Same as when checking node NPM

  • Check the version number with the command at the command line

    $ nrm --version

  • If the version number is displayed, the installation is successful

The use of NRM

  • NRM stores several mirror source addresses
  • We’re going to pick a fast one to use

Detects the source address of the mirror

  • We use the command directly from the command line to view the network speed of all mirror source addresses

    # displays the network speed of the source ADDRESS of the NRM mirror

    $ nrm test

Changing the Mirror Source

  • Once we’re done, we’ll figure out which one is faster

  • We just use the command to switch the source address of the mirror

    # indicates switching to the source address of taobao mirror

    $ nrm use taobao

More recommended

  • JQuery (jQuery)
  • JQuery (中 文)
  • JQuery (part 1)
  • JavaScript Learning Notes (30) — Design Patterns