Skip to main content

Create a Document

Documents are groups of pages connected through:

  • a sidebar
  • previous/next navigation
  • versioning

Create your first Doc

Create a Markdown file at docs/hello.md:

docs/hello.md
# Hello

This is my **first Docusaurus document**!

A new document is now available at http://localhost:3000/docs/hello.

Configure the Sidebar

Docusaurus automatically creates a sidebar from the docs folder.

Add metadata to customize the sidebar label and position:

docs/hello.md
---
sidebar_label: 'Hi!'
sidebar_position: 3
---

# Hello

This is my **first Docusaurus document**!

It is also possible to create your sidebar explicitly in sidebars.js:

sidebars.js
export default {
tutorialSidebar: [
'intro',
'hello',
{
type: 'category',
label: 'Tutorial',
items: ['tutorial-basics/create-a-document'],
},
],
};
JS
// 导入模块
const express = require('express');
const app = express();
const PORT = 8080;

// 中间件:解析 JSON 数据
app.use(express.json());

// 模拟数据库数据
let users = [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 3, name: 'Charlie', age: 35 },
];

// 获取所有用户 (GET)
app.get('/api/users', (req, res) => {
res.json(users);
});

// 根据 ID 获取用户 (GET)
app.get('/api/users/:id', (req, res) => {
const userId = parseInt(req.params.id, 10);
const user = users.find(u => u.id === userId);
if (user) {
res.json(user);
} else {
res.status(404).send('用户未找到');
}
});

// 创建新用户 (POST)
app.post('/api/users', (req, res) => {
const newUser = {
id: users.length + 1, // 简单的 ID 生成方式
name: req.body.name,
age: req.body.age,
};
users.push(newUser);
res.status(201).json(newUser); // 返回新创建的用户
});

// 更新用户 (PUT)
app.put('/api/users/:id', (req, res) => {
const userId = parseInt(req.params.id, 10);
const user = users.find(u => u.id === userId);

if (user) {
user.name = req.body.name || user.name;
user.age = req.body.age || user.age;
res.json(user);
} else {
res.status(404).send('用户未找到');
}
});

// 删除用户 (DELETE)
app.delete('/api/users/:id', (req, res) => {
const userId = parseInt(req.params.id, 10);
const userIndex = users.findIndex(u => u.id === userId);

if (userIndex !== -1) {
const deletedUser = users.splice(userIndex, 1);
res.json(deletedUser);
} else {
res.status(404).send('用户未找到');
}
});

// 启动服务器
app.listen(PORT, () => {
console.log(`服务器已启动,访问地址:http://localhost:${PORT}`);
});