Understanding Node.JS - Basic Concepts
This is project is about practicing the basic concepts behind Node.JS that were learned during the first module of GoStack 10 - Bootcamp.
-
Install dependencies:
yarn
-
Start the back-end:
yarn dev
-
It's possible to test the GET methods through the browser, but for the other methods I recommend using applications such as Insomnia or Postman.
-
Basic Concepts of a REST API
- How does it work?
- Resquest & Response flow:
- Request is made by the client (e.g.: browser)
- Response is returned - by the server - through a data structure (e.g.: JSON)
- Client gets the response and processes the result
- Resquest & Response flow:
- How does it work?
-
Routes use HTTP methods
- GET:
http://myapi.com/users
- Looks for data
- POST:
http://myapi.com/users
- Creates data
- PUT:
http://myapi.com/users/1
- Updates a piece data
- DELETE:
http://myapi.com/users/3
- Deletes a piece of data
- GET:
-
Benefits
- Multiple clients (front-end) using the same back-end
- Standard communication protocol
- Same structure for web/mobile/public API
- Communication with external services
-
Request Content
-
GET:
http://myapi.com/company/1/users?page=2
- routes:
/company
&/users
- route params:
/1/
- query params:
?page=2
- routes:
-
POST:
http://myapi.com/company/1/users?page=2
- User data to be created is sent through the body/request body (PUT/POST only)
- More detailed/sensitive data
- User data to be created is sent through the body/request body (PUT/POST only)
-
Headers: Additional information that is not related to the request's content (e.g.:
{ "locale": "pt_BR }
)
-
- HTTP Codes: Every response (back-end -> front-end) takes with it a HTTP code, that represents the response's status.
- 1xx: INFORMATIONAL
- 2xx: SUCCESS
- 200: success
- 201: created
- 3xx: REDIRECTION
- 301: moved permanently
- 302: moved
- 4xx: CLIENT ERROR (there is something wrong with the request)
- 400: bad request
- 401: unauthorized
- 404: not found
- 5xx: SERVER ERROR
- 500: internal server error
-
Creating the application
yarn init -y
-
package.json
- It has the important functionality, among many others, of saving the references to the project dependencies that were installed.
-
index.js
const express = require('express'); // exports a function // Notice that express is a function that // returns an object with many functionalities console.log(express);
-
There are three types of parameters that can be part of a request:
- Query params ->
?name=John
const { name } = request.query
- Route params ->
/users/1
->/users/:id
const { id } = request.params
- Body/Request body ->
{ "name": "John", "age": 27 }
const { name, age } = request.body
- Query params ->
-
Middlewares (Global and Local)
- They are a fundamental part of every Express application!
- It's a function that gets
request
andresponse
as parameters (among others, e.g.:next
) and transforms/check data.
- Global
- It's always invoked, no matter what route is being triggered.
server.use((req, res) => { console.time('Request'); console.log(`Method: ${req.method}; URL: ${req.url}`); next(); console.timeEnd('Request'); });
- Local
-
It's applied directly to routes
-
You can add as many middlewares as you feel necessary.
-
Middlewares can change the
request
andresponse
parameters.
-
Debugging the application
- Learned how to use breakpoints in VSCode.
- Understand variables' values and what is happening behind the scenes in the application.
Made with ♥ by Yago!