自学WEB后端03-Node.js 语法

学习后端路线:

JavaScript 基础语法 + Node,js 内置 API 模块 (fs、 path、 http等) + 第三方 API 模块 (express、mysql等)

今天主要回顾下Node.js 语法

Node.js 是基于 Chrome V8 引擎的 JavaScript 运行环境,它提供了一种能够在服务器端运行 JavaScript 代码的平台。

1.以下是 Node.js 的基本语法:

1. 输出文本:使用 console.log() 方法输出文本信息到控制台窗口。

   ```
   console.log("Hello, World!");
   ```

2. 变量声明:使用 var、const 或 let 关键字声明变量。

   ```
   var a = 10; // 全局变量
   let b = "Hello"; // 块级作用域
   const c = true; // 常量
   ```

3. 数据类型:Node.js 支持基本的数据类型(如字符串、数字和布尔值)以及对象、数组和函数等复合类型。

   ```
   var str = "Hello"; // 字符串
   var num = 123; // 数字
   var bool = true; // 布尔值
   var obj = { name: "Alice", age: 20 }; // 对象
   var arr = [1, 2, 3]; // 数组
   function add(a, b) { return a + b; } // 函数
   ```

4. 条件语句:使用 if、else、switch 等语句根据条件来执行不同的代码分支。

   ```
   var x = 10;
   if (x > 0) {
     console.log("x is positive");
   } else if (x < 0) {
     console.log("x is negative");
   } else {
     console.log("x is zero");
   }

   var color = "red";
   switch (color) {
     case "red":
       console.log("the color is red");
       break;
     case "green":
       console.log("the color is green");
       break;
     default:
       console.log("unknown color");
   }
   ```

5. 循环语句:使用 for、while 等语句来实现循环操作。

   ```
   for (var i = 0; i < 10; i++) {
     console.log(i);
   }

   var j = 0;
   while (j < 10) {
     console.log(j);
     j++;
   }
   ```

6. 函数定义:使用 function 关键字定义函数,并使用 return 语句返回函数的值。

   ```
   function add(a, b) {
     return a + b;
   }

   var sum = add(1, 2);
   console.log(sum);
   ```

2.常用的 Node.js 语法和功能

除了上述提到的一些基本语法,还有其他一些常用的 Node.js 语法和功能,以下是其中的一些:

1. 模块引入:使用 require 关键字引入模块,以便在代码中使用其功能。

   ```javascript
   const fs = require("fs"); // 引入文件系统模块
   const http = require("http"); // 引入 HTTP 模块
   const express = require("express"); // 引入 Express 框架
   ```

2. 回调函数:在 Node.js 中,回调函数是一种常见的处理异步操作的方式,可以通过回调函数来处理异步操作的结果。

   ```javascript
   const fs = require("fs");
   fs.readFile("file.txt", "utf8", function (err, data) {
     if (err) {
       console.error(err);
     } else {
       console.log(data);
     }
   });
   ```

3. 异步操作:Node.js 是以非阻塞 I/O 操作为特点的,可以利用回调函数或异步函数来执行耗时的操作,而不会阻塞其他代码的执行。

   ```javascript
   setTimeout(function () {
     console.log("Delayed message");
   }, 2000); // 2 秒后执行

   fs.writeFile("file.txt", "Hello, World!", function (err) {
     if (err) {
       console.error(err);
     } else {
       console.log("File written successfully");
     }
   });
   ```

4. 箭头函数:箭头函数是一种简化了的函数定义语法,在某些情况下可以让代码更加简洁。

   ```javascript
   const add = (a, b) => a + b;
   console.log(add(2, 3)); // 输出 5

   const square = (x) => x * x;
   console.log(square(4)); // 输出 16
   ```

5. Promise 和 async/await:Promise 是用于处理异步操作的对象,而 async/await 是基于 Promise 的语法糖,可以更加方便地编写和管理异步代码。

   ```javascript
   function fetchData() {
     return new Promise((resolve, reject) => {
       setTimeout(() => {
         resolve("Data fetched successfully");
       }, 2000);
     });
   }

   async function process() {
     try {
       const data = await fetchData();
       console.log(data);
     } catch (error) {
       console.error(error);
     }
   }

   process(); // 输出:Data fetched successfully
   ```

这些是一些常见的 Node.js 语法和功能,有助于开发出更复杂和高效的应用程序。当然,Node.js 提供了丰富的核心模块和第三方模块,可以根据具体需求进行进一步的学习和探索。

猜你喜欢

转载自blog.csdn.net/leoysq/article/details/133317974
今日推荐