如何在 Node.js 中编写 API
编写 Node.js API 涉及创建服务器和定义 HTTP 路由以处理来自客户端的请求。以下是如何使用 Node.js 编写 API 的步骤:
1. 创建 Node.js 服务器
使用 Node.js 的 http
模块创建服务器:
<code class="javascript">const http = require(\'http\'); const server = http.createServer((req, res) => { // 处理请求... }); server.listen(3000, () => { console.log(\'Server listening on port 3000\'); });