Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,使用异步事件驱动和非阻塞 I/O 模型,使其轻量级并高效。

快速开始

以下是一些 Node.js 快速入门的步骤:

  1. 安装 Node.js:前往 Node.js 官网 下载并安装 Node.js。
  2. 编写第一个 Node.js 脚本:在命令行中创建一个名为 hello.js 的文件,并写入以下内容:
console.log('Hello, Node.js!');
  1. 运行脚本:在命令行中执行 node hello.js,你应该能看到 "Hello, Node.js!" 的输出。

常用模块

  • Express:一个快速、灵活的 Web 应用框架。
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, Express!');
});

app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000');
});
  • Mongoose:一个流行的 MongoDB 对象建模工具。
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
  name: String,
  email: String
});

const User = mongoose.model('User', UserSchema);

const user = new User({ name: 'Alice', email: 'alice@example.com' });
user.save()
  .then(() => console.log('User saved'))
  .catch(err => console.error(err));

实践资源

图片

Node.js

以上内容为 Node.js 入门教程,更多详细内容请参考 Node.js 教程