forked from Techtonica/curriculum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
265 changed files
with
13,081 additions
and
5,846 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
### Welcome to the Techtonica curriculum! | ||
|
||
Techtonica is a non-profit project of SocialGood that does free tech training and job placement for unemployed or underemployed folks. For more information, please see our website: [Techtonica.org](http://techtonica.org) | ||
Techtonica is a non-profit project of SocialGood that offers a software engineering training with a need-based, sliding-scale, subsidized tuition and stipend scholarships for intensive tech training and job placement for unemployed or underemployed folks. For more information, please see our website: [Techtonica.org](http://techtonica.org) | ||
|
||
Program participants will learn to do full-stack web development using JavaScript. What bootcamps and Techtonica have in common is the fact that they are intensive training. However, bootcamps can be pretty passive classroom learning and they cost \$15,000+ for the students. | ||
|
||
|
@@ -14,8 +14,8 @@ If you would like to contribute to the curriculum, please [sign up here](https:/ | |
|
||
**Get started:** | ||
|
||
- Choose an issue from the [TO-DO](https://github.com/Techtonica/curriculum/projects/2) column that seems right for you & leave a comment for Techtonica staff. If you know someone connected to Techtonica, they may be able to add you to our slack channel. If you are here for GHC or Hacktoberfest, message Alina, Leah, or TaLea and say you'd like to pick up the issue. | ||
- In the issue, you should find a link for a lesson outline and for corresponding slides. | ||
- Choose an issue from the [TO-DO](https://github.com/Techtonica/curriculum/projects/2) column that seems right for you & leave a comment for Techtonica staff. If you know someone connected to Techtonica, they may be able to add you to our slack channel. If you are here for GHC or Hacktoberfest, message Cristina, Ruthie or Sarah and say you'd like to pick up the issue. | ||
- In the issue, you should find a link for a lesson outline and for our resources about the topic. | ||
- If it is in the ["TO-DO"](https://github.com/Techtonica/curriculum/projects/2) column, that means it is available, even if there's an "assignee". | ||
- To **create an outline**, fork the repo or commit directly onto the main branch. [Here is a topic outline template that explains what we need for each section](./_templates/topic-outline.md). | ||
- To **edit a slideshow**, you'll need to request edit permission by clicking View Only > Request Edit Access, or message Alina. If you are logged into an account ending in @gmail.com, you can simply make a copy, edit, and transfer ownership to [email protected] when you finish. Either way, save on top of the sample template used in the slide, and cut any slides you find unnecessary. | ||
|
@@ -116,7 +116,7 @@ Participants should memorize [common JavaScript built-in functions](/javascript/ | |
1. [Portfolio Project: Personal Branding](/personal-brand/personal-branding.md) | ||
1. [Reading/Writing Documentation](/reading-and-writing-documentation/documentation.md) | ||
1. [Writing Readable Code](/writing-readable-code/writing-readable-code.md) | ||
1. [Deploy your Portfolio Project: Deploying Client-Side Apps with Netlify](/deploying/deploying-1-personal-site.md) | ||
1. [Deploy your Portfolio Project: Deploying Client-Side Apps with Netlify](/deploying/deploying-1-personal-site-to-netlify.md) | ||
1. [Portfolio Project: Collaboration and Review](/projects/portfolio/portfolio-webpage-3.md) | ||
|
||
### Week 3 - JavaScript Fundamentals | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
- What is a Restful API? | ||
- What is JSON? | ||
|
||
|
||
### Prerequisites | ||
|
||
Here are links to lessons that should be completed before this lesson: | ||
|
@@ -72,6 +71,16 @@ Again, REST is an acronym. It stands for **RE**presentational **S**tate **T**ran | |
|
||
Basically, a REST API is simply a style of architecture that we use when we design any type of networked applications. It can be implemented in any language, but for this lesson, we will be using NodeJS. | ||
|
||
#### Key principles of a RESTful API include: | ||
|
||
**Statelessness:** The server does not store any client state between requests. Each request from a client must contain all the information necessary to understand and process that request. | ||
|
||
**Uniform Interface:** The API should have a consistent and standardized way of interacting with resources. This typically includes using standard HTTP methods like GET (retrieve), POST (create), PUT (update), DELETE (remove), etc. | ||
|
||
**Representation:** Resources are represented in various formats like JSON (JavaScript Object Notation), XML (eXtensible Markup Language), or HTML (HyperText Markup Language). | ||
|
||
**Self-contained:** Each resource should include enough information to understand how to interact with it. Hypermedia controls (e.g., links) are often used to guide clients through the API. | ||
|
||
### HTTP Methods | ||
|
||
When you want to make a request to your API, if that one follows RESTful conventions, you will want to use specific HTTP Methods. Here are the most common ones; the ones we will be using below in the guided practice: | ||
|
@@ -485,10 +494,8 @@ So, the same way, you can setup the second GET method, as planned: | |
| ----------- | ----------- | | ||
| `/invoices` | `GET` | | ||
|
||
With Express, routing is as easy as chaining the HTTP method to the `route()` method, taking care of putting the route you want to lookup inside of the `route()` method parenthesis. | ||
|
||
```javascript | ||
app.route('/invoices').get((req, res) => { | ||
app.get('/invoices', (req, res) => { | ||
res.status(200).send(invoices); | ||
}); | ||
``` | ||
|
@@ -505,7 +512,7 @@ However, the beauty of a RESTful API is that we want to use the possibility to f | |
So, to create these, simply add another routing to the `app`, but this time we need to fetch the ID that's included in the parameters. If you recall your server lessons, this can be done by accessing the `req.params` object. We then should iterate through all records until we find the record that has the same `id` because that is the property that we are looking for. Of course, you could use any of the object's properties, here. You would just need to adjust the route appropriately, but we will not cover this for now. | ||
|
||
```javascript | ||
app.route('/customers/:id').get((req, res) => { | ||
app.get('/customers/:id', (req, res) => { | ||
let customer_id = req.params.id; | ||
let status = 400; | ||
let response = 'Unable to fetch data!'; | ||
|
@@ -517,7 +524,7 @@ app.route('/customers/:id').get((req, res) => { | |
res.status(status).send(response); | ||
}); | ||
|
||
app.route('/invoices/:id').get((req, res) => { | ||
app.get('/invoices/:id', (req, res) => { | ||
let invoice_id = req.params.id; | ||
let status = 400; | ||
let response = 'Unable to fetch data!'; | ||
|
@@ -633,6 +640,71 @@ Finally, for the `DELETE` method: | |
|
||
And there you go! You now have a complete functioning RESTful API with full CRUD functionality! | ||
|
||
```javascript | ||
const express = require('express'); | ||
const app = express(); | ||
const PORT = 3000; | ||
|
||
// Middleware to parse JSON data in the request body | ||
app.use(express.json()); | ||
|
||
// Sample data: Replace this with a database in a real application | ||
let customers = [ | ||
{ id: 1, name: 'John Doe', email: '[email protected]' }, | ||
{ id: 2, name: 'Jane Smith', email: '[email protected]' }, | ||
]; | ||
|
||
// GET all customers | ||
app.get('/customers', (req, res) => { | ||
res.json(customers); | ||
}); | ||
|
||
// GET a single customer by ID | ||
app.get('/customers/:id', (req, res) => { | ||
const customerId = parseInt(req.params.id); | ||
const customer = customers.find((c) => c.id === customerId); | ||
if (customer) { | ||
res.json(customer); | ||
} else { | ||
res.status(404).json({ error: 'Customer not found' }); | ||
} | ||
}); | ||
|
||
// POST a new customer | ||
app.post('/customers', (req, res) => { | ||
const { name, email } = req.body; | ||
const newCustomer = { id: customers.length + 1, name, email }; | ||
customers.push(newCustomer); | ||
res.status(201).json(newCustomer); | ||
}); | ||
|
||
// PUT (update) an existing customer by ID | ||
app.put('/customers/:id', (req, res) => { | ||
const customerId = parseInt(req.params.id); | ||
const { name, email } = req.body; | ||
const customerIndex = customers.findIndex((c) => c.id === customerId); | ||
if (customerIndex !== -1) { | ||
customers[customerIndex] = { id: customerId, name, email }; | ||
res.json(customers[customerIndex]); | ||
} else { | ||
res.status(404).json({ error: 'Customer not found' }); | ||
} | ||
}); | ||
|
||
// DELETE a customer by ID | ||
app.delete('/customers/:id', (req, res) => { | ||
const customerId = parseInt(req.params.id); | ||
customers = customers.filter((c) => c.id !== customerId); | ||
res.json({ message: 'Customer deleted successfully' }); | ||
}); | ||
|
||
// Start the server | ||
app.listen(PORT, () => { | ||
console.log(`Server running on http://localhost:${PORT}`); | ||
}); | ||
|
||
``` | ||
|
||
</details> | ||
|
||
## PART III: Reference, Practice and Supplemental Materials | ||
|
@@ -693,12 +765,9 @@ Because practice makes perfect, especially with REST APIs, work with you pair on | |
|
||
- If you want to follow along another type of guided practice, you can have a look at how to [Build a Node.js API in Under 30 Minutes](https://www.freecodecamp.org/news/building-a-simple-node-js-api-in-under-30-minutes-a07ea9e390d2/) blog post. | ||
- [5 Minute Intro to REST APIs](https://www.newline.co/@bchiang7/5-minute-intro-to-rest-apis--b5081674) | ||
|
||
- Another great resource is the [What is a RESTful API](https://www.youtube.com/watch?v=Q-BpqyOT3a8) video tutorial by Traversy Media. | ||
- To learn about the Guiding principles of REST: | ||
- [restfulapi.net](https://restfulapi.net/rest-architectural-constraints/) | ||
- [Wikipidia](https://en.wikipedia.org/wiki/Representational_state_transfer#cite_note-Fielding-Ch5-3) | ||
- [Dinesh on Java](https://www.dineshonjava.com/what-is-rest-and-rest-architecture-and-rest-constraints/), to show you APIs can be built with any language but keep their familiarity. | ||
- [Future Vision on Medium](https://medium.com/future-vision/what-are-the-constraints-of-rest-and-how-they-saved-the-internet-6fb8503138ab) | ||
- [A visual blog post](https://blog.appscrip.com/what-is-restful-api-key-constraints-use-cases/) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.