JavaScript? REST API ???
REST API(Representational State Transfer Application ????? ?????)? ???? ??????? ???? ? ?? ?????. ? ??? ????? ? ? ?? ? ??? ?? ???? JavaScript?? REST API? ???? ??? ???? ? ??? ? ????.
1. REST API? ??????
REST API? ???? ?????(?: ???? ?? ??? ?)? ??? ???? ???? ????? ??? ? ????. ?? HTTP ??? ???? ??? ????? ????.
?? ??
- ???: ?????? ?????(?: ??? ???? ?? /users).
-
HTTP ??:
- GET: ???? ?????.
- POST: ? ???? ?????.
- PUT: ?? ???? ???????.
- ??: ???? ?????.
- ??? ??: JSON? ????? ??? ??? ?????.
-
HTTP ?? ??:
- 200 OK: ??.
- 201 ???: ???? ???????.
- 400 ??? ??: ?????? ?????.
- 404 ?? ? ??: ???? ?? ? ????.
- 500 ?? ?? ??: ?? ?????.
2. ?? ? ??
-
??????:
- ????(fetch ?? axios ?????? ?? JavaScript).
- https://jsonplaceholder.typicode.com? ?? API? ??? ??? ???.
-
???:
- Node.js? ???? Express ?????? ?????.
3. ????? ??? REST API ??
JavaScript? REST API? ?????? ?? fetch() API? axios? ?? ?? ?????? ?????.
fetch()? ???? ??? ????
REST API?? ???? ???? ??? ??? ????.
// Fetch data from an API const fetchUsers = async () => { try { const response = await fetch('https://jsonplaceholder.typicode.com/users'); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const users = await response.json(); // Parse JSON data console.log(users); } catch (error) { console.error('Error fetching users:', error); } }; fetchUsers();
??:
- fetch(url): HTTP ??? ???.
- response.json(): ??? JSON ???? ?????.
- ?? ??? ???? ??? ??? ??? ???? ?? try...catch? ???? ?????.
POST? ??? ??
? ???? ????? fetch() API? ?? POST ???? ?????.
const createUser = async () => { try { const response = await fetch('https://jsonplaceholder.typicode.com/users', { method: 'POST', // HTTP method headers: { 'Content-Type': 'application/json', // Specify JSON format }, body: JSON.stringify({ // Convert JavaScript object to JSON name: 'Jane Doe', email: 'jane.doe@example.com', }), }); const newUser = await response.json(); // Parse JSON response console.log(newUser); } catch (error) { console.error('Error creating user:', error); } }; createUser();
?? ???:
- method ??? HTTP ???? ?????.
- ?? ??? ??? ??? ???? ? ?????.
- ???? JSON ????? ???? ????.
4. ?? ??? REST API ??
?????? ????? Express ?????? ??? Node.js? ???? REST API? ?????.
?? ??
- Node.js ??: Node.js? ???????.
- ? ???? ???:
// Fetch data from an API const fetchUsers = async () => { try { const response = await fetch('https://jsonplaceholder.typicode.com/users'); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const users = await response.json(); // Parse JSON data console.log(users); } catch (error) { console.error('Error fetching users:', error); } }; fetchUsers();
??? REST API ???
??? ?? REST API ??? ????.
const createUser = async () => { try { const response = await fetch('https://jsonplaceholder.typicode.com/users', { method: 'POST', // HTTP method headers: { 'Content-Type': 'application/json', // Specify JSON format }, body: JSON.stringify({ // Convert JavaScript object to JSON name: 'Jane Doe', email: 'jane.doe@example.com', }), }); const newUser = await response.json(); // Parse JSON response console.log(newUser); } catch (error) { console.error('Error creating user:', error); } }; createUser();
??:
- ????: app.use(express.json())? ???? JSON ??? ?? ?????.
-
??:
- GET /users: ?? ???? ?????.
- GET /users/:id: ?? ???? ?????.
- POST /users: ? ???? ?????.
- PUT /users/:id: ??? ????? ???????.
- DELETE /users/:id: ???? ?????.
5. REST API ???
Postman? ?? ??? ?? ?? ??? ????? ???? API? ???? ? ????.
?? ??? ??
- ???? Postman? ?????.
- ? ?? ???:
- GET http://localhost:3000/users: ?? ???? ?????.
- POST http://localhost:3000/users: JSON ???? ???? ?????.
? ??
mkdir rest-api-demo cd rest-api-demo npm init -y npm install express
6. REST API ?? ?? ??
- ?? ?? ????? ??? ?????(?: /data ?? /users).
- ???? ??? ??? ???? ???? ?? ??? ??? ?????.
- ???? HTTP ?? ??? ????.
- Swagger ?? Postman? ?? ??? ???? API? ??????.
? ?? ?? ???
??
REST API? ?? ? ??? ?????. ????? ?? ?? ? ???? JavaScript? REST API? ?? ???? ??? ??? ?????? ?? ? ??? ?? ??? ?? ??? ?? ? ????. ??? ?????. ?? API? ???? ??? ?? Node.js ? Express? ???? ???? API? ?????.
? ???? ?? ??? ?? ????? ??? ?????!
? ??? JavaScript? REST API ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

JavaScript? ??? ?? ????? ??? ?? ??? ??? ?? ?? ?? ????? ?? ???? ???? ?????. ??? ?? ???? ?? ??? ?? ??? ???? ???? ?? ?? ???? ???? ?????. ?? ??, ??? ? ?? ???? ??? (? : ??? null? ??) ?? ??? ????? ??????. ??? ??? ???? ??? ??? ????. closure?? ?? ??? ?? ??; ? ??? ??? ?? ?? ???? ?? ???? ????. V8 ??? ?? ???, ?? ??, ??/?? ???? ?? ??? ?? ??? ??? ????? ?? ??? ?? ??? ????. ?? ?? ???? ??? ??? ??? ??? ???? ????? ?? ?? ???? ?? ???????.

Node.js?? HTTP ??? ???? ? ?? ???? ??? ????. 1. ?? ????? ????? ??? ??? ? ?? ????? ?? ?? ? https.get () ??? ?? ??? ??? ? ?? ????? ?? ??? ?????. 2.axios? ??? ???? ? ?? ??????. ??? ??? ??? ??? ??? ??? ???/???, ?? JSON ??, ???? ?? ?????. ??? ?? ??? ????? ?? ????. 3. ?? ??? ??? ??? ??? ???? ???? ??? ??? ???? ?????.

JavaScript ??? ??? ?? ?? ? ?? ???? ????. ?? ???? ???, ??, ??, ?, ???? ?? ? ??? ?????. ?? ????? ?? ?? ? ? ??? ????? ?? ??? ??? ????. ??, ?? ? ??? ?? ?? ??? ??? ??? ???? ??? ??? ???? ??? ?? ??? ????. ?? ? ????? ??? ???? ? ??? ? ??? TypeofNull? ??? ?????? ??? ? ????. ? ? ?? ??? ???? ?????? ????? ???? ??? ???? ? ??? ? ? ????.

?????, JavaScript ???! ?? ? JavaScript ??? ?? ?? ?????! ?? ?? ??? ??? ??? ? ????. Deno?? Oracle? ?? ??, ??? JavaScript ?? ??? ????, Google Chrome ???? ? ??? ??? ???? ?????. ?????! Deno Oracle? "JavaScript"??? ????? Oracle? ?? ??? ??? ??????. Node.js? Deno? ??? ? Ryan Dahl? ??? ?????? ???? ????? JavaScript? ??? ???? Oracle? ????? ???? ?????.

?? JavaScript ??? ??? ??? ?????? ?? ??? ?? ?? ??? ?? ???? ????. 1. ??? ???? ???? ?? ??? ?? ? ? ???? ??? ??? ?? ? ?? ????? ?????. 2. Angular? ?????? ??? ?? ???? ? ?? ?? ??? ??? ??? ???? ?????. 3. VUE? ???? ?? ??? ???? ?? ?? ??? ?????. ?? ?? ?? ??, ? ??, ???? ???? ? SSR? ???? ??? ??? ??? ???? ? ??? ?????. ???, ??? ??? ??? ????? ????. ??? ??? ??? ??? ?? ????.

iife (?? invokedfunctionexpression)? ?? ??? ???? ?? ????? ??? ???? ?? ??? ????? ?? ??? ? ?????. ??? ?? ?? ??? ???? ? ?? ??? ??? ?? (function () {/code/}) ();. ?? ???? ??? ?????. 1. ?? ??? ??? ?? ???? ?? ??? ??? ?????. 2. ?? ??? ??? ???? ?? ?? ??? ????. 3. ?? ?? ??? ????? ?? ???? ???????? ?? ? ??. ???? ?? ???? ?? ??? ES6 ??? ??? ??? ?? ? ??? ????? ??? ? ???? ???????.

??? JavaScript?? ??? ??? ?????? ?? ???????. ?? ??, ?? ?? ? ??? ??? ?? ????? ????? ?????. 1. ?? ??? ??? ????? ???? ??. ()? ?? ??? ??? ?????. ?. ()? ?? ??? ?? ??? ??? ?? ? ? ????. 2. ?? ??? .catch ()? ???? ?? ??? ??? ?? ??? ??????, ??? ???? ???? ????? ??? ? ????. 3. Promise.all ()? ?? ????? (?? ?? ?? ? ??????? ??), Promise.Race () (? ?? ??? ?? ?) ? Promise.AllSettled () (?? ??? ???? ??)

Cacheapi? ?????? ?? ???? ??? ???? ???, ?? ??? ??? ?? ???? ? ??? ?? ? ???? ??? ??????. 1. ???? ????, ??? ??, ?? ?? ?? ???? ???? ??? ? ????. 2. ??? ?? ?? ??? ?? ? ? ????. 3. ?? ?? ?? ?? ?? ??? ??? ?? ?????. 4. ??? ???? ?? ?? ???? ?? ?? ?? ?? ?? ???? ?? ?? ??? ??? ? ????. 5. ?? ???? ??, ??? ??? ? ??? ??, ?? ??? ? ?? ???? ???? ???? ? ?? ?????. 6.?? ??? ?? ?? ?? ??, ???? ?? ? HTTP ?? ????? ?????? ???????.
