1. How to run JS with Node?

  • 1. Open the terminal and enter Node, as shown below:

Run: Input JS code, press Enter to run;

Exit: CTRL + D (CTRL + C twice)

  • 2. Use files

Open VScode (enter code hello.js and press Enter to jump to vscode to create the hello.js file) and enter the JS code

Run: Terminal type node hello.js

2. Initialize the NodeJS project

  • 1. Create a folder named my-Node-app
  • 2. Enter NPM init and press Enter to generate the package.json file as prompted

3. Use scripts in NPM to automate development tasks

    1. Add scripts to package.json file
"scripts": {  
   "start": "node ./dist/index.js",  
    "test": "jest",  
    "build": "tsc",  
    "lint": "eslint"  
  },
Copy the code

    1. Enter NPM run start on the terminal
    1. Install dependency packages:

NPM install XXX

    1. Package – the lock. The json file

NPM outdated has update dependencies

Yellow means the latest version is available, but not in the updatable range of your package.json file

The red color indicates that it exists within the specified range of your package.json file and can be updated with confidence.

NPM Update All red packages will be updated to the latest version

  1. Write a Web server using NodeJS
  • 1. Create file index.js
const http = require('http'); Const hostname = '127.0.0.1; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type','text/plain'); res.end('hello,world'); }); Server. Listen (port, the hostname, () = > {the console. The log (` server running on http://${hostname} : ${port} / `)});Copy the code
  • 2. In the terminal window, run the following command: The node index.js terminal displays the following information: The server is running at http://127.0.0.1:3000/

  • 3. Open any preferred Web browser and visit http://127.0.0.1:3000. If the browser displays the string hello,world, the server is running.