- Clone the project repository to your computer:
git clone <projectURL>cdinto the project directory- Run
npm installto install the project dependices- Run
node <script>.jsto start a script. For example:node simple-api-example_1.js
- Or run
npm run ex1
To get the most out of this repo, please read the following tutorial: https://fofocode.dev/article/?post=7
node simple-api-example_1.js OR npm run ex1
Powershell (Windows)
$response = Invoke-RestMethod 'http://localhost:3000' -Method 'GET' -Headers $headers
$response | ConvertTo-Json
Terminal / CURL (CURL, macOS and Linux)
curl --location --request GET 'http://localhost:3000'
Look Out World! Here We Come!
node simple-api-example_2.js OR npm run ex2
Powershell (Windows)
JSON
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$body = "{`"someKey`":`"Some Data`"}"
$response = Invoke-RestMethod 'http://localhost:3000/submit-data' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
URL FORM ENCODED
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/x-www-form-urlencoded")
$body = "someKey=Some%20Data"
$response = Invoke-RestMethod 'http://localhost:3000/submit-data' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
Terminal / CURL (CURL, macOS and Linux)
JSON
curl --location --request POST 'http://localhost:3000/submit-data' \
--header 'Content-Type: application/json' \
--data-raw '{
"someKey": "Some Data"
}'
URL FORM ENCODED
curl --location --request POST 'http://localhost:3000/submit-data' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'someKey=Some Data'
You sent the following data:
{"someKey":"Some Data"}
node simple-api-example_3.js OR npm run ex3
GET: http://localhost:3000/get-data POST: http://localhost:3000/submit-data PUT: http://localhost:3000/update-data DELETE: http://localhost:3000/delete-data
Powershell (Windows)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/x-www-form-urlencoded")
$body = "movieKey=2&newMovieTitle=My%20New%20Movie!"
$response = Invoke-RestMethod 'http://localhost:3000/update-data' -Method 'PUT' -Headers $headers -Body $body
$response | ConvertTo-Json
Terminal / CURL (CURL, macOS and Linux)
curl --location --request PUT 'http://localhost:3000/update-data' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'movieKey=2' \
--data-urlencode 'newMovieTitle=My New Movie!'
{
"status": "Updated Database Successfully",
"databaseData": [
...
...
"My New Movie!",
...
]
}
Powershell (Windows)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/x-www-form-urlencoded")
$body = "movieKey=0"
$response = Invoke-RestMethod 'http://localhost:3000/delete-data' -Method 'DELETE' -Headers $headers -Body $body
$response | ConvertTo-Json
Terminal / CURL (CURL, macOS and Linux)
curl --location --request DELETE 'http://localhost:3000/delete-data' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'movieKey=0'
{
"status": "Deleted ... from Database Successfully",
"databaseData": [
...
]
}