环境准备
确保已安装 Node.js(建议 18+),在终端检查版本:
node -v
npm -v
初始化项目
mkdir my-api && cd my-api
npm init -y
npm install express
写出第一个接口
创建 app.js:
import express from 'express';
const app = express();
app.use(express.json());
app.get('/api/hello', (req, res) => {
res.json({ message: 'Hello from Express!' });
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`服务已启动: http://localhost:${PORT}`);
});
启动:node app.js,访问 http://localhost:3000/api/hello 即可看到返回的 JSON。
处理 POST 请求
let todos = [];
app.post('/api/todos', (req, res) => {
const { title } = req.body;
const item = { id: todos.length + 1, title };
todos.push(item);
res.status(201).json(item);
});
小结
你已经用 Express 写出了 GET 与 POST 接口。后续可以引入数据库、路由拆分与中间件,逐步完善后端架构。