项目模板·Pro
无依赖的 Node.js JSON API
基于 node:http 的笔记 API:创建、列出、修改、删除,带输入校验和 JSON 错误信息,数据保存到文件。无需安装任何东西:在 server.js 上点 Run,应用自带的 Node.js 会在 localhost:3000 启动它。附带测试。
安装它需要 PocketCode Pro。
如果你的 Android 设备上装了 PocketCode,这会在应用里打开此页面。
预览
文件
README.md
package.json
server.js
src/app.js
src/store.js
test/api.test.jsREADME.md
# Notes API on Node.js, no dependencies A JSON API for notes built on `node:http`: create, list, read, update and delete, with input checks, clear error messages and a request log. Notes are saved to `data/notes.json` after every change. There is nothing to install: no npm packages, no framework. ## Run it in PocketCode 1. Open `server.js` and tap Run. The app's own Node.js starts it; the Run panel shows `Notes API on http://localhost:3000`. 2. Open `http://localhost:3000` in the DevBrowser (or in the phone's browser) to see the list of endpoints. 3. Try the API from the terminal, or from any HTTP client: ```sh curl -X POST localhost:3000/api/notes -H 'content-type: application/json' -d '{"text":"Buy milk"}' curl localhost:3000/api/notes ``` 4. Tap Stop in the Run panel to end it. You don't need the Linux environment to run the server. `curl` and `npm` are in the terminal's Linux environment with Node.js. ## Run the tests - Open `test/api.test.js` and tap Run. - Or, in the terminal: `npm test`. The tests start the server on a free port with the notes in memory, so they never touch `data/notes.json`. ## Endpoints | Method and path | Body | Answer | |---|---|---| | `GET /health` | | `{"ok": true}` | | `GET /api/notes` | | All notes, oldest first | | `POST /api/notes` | `{"text": "..."}` | 201 and the new note | | `GET /api/notes/:id` | | The note, or 404 | | `PATCH /api/notes/:id` | `{"text"?: "...", "done"?: true}` | The updated note |此预览已截短。安装后可获得完整内容。
1.0.0 的变更
首个版本。