Git Product home page Git Product logo

node-2's Introduction

Node Two

In this lecture we talk about Top Level Middleware, Using Postman, and using Axios in our API.

Examples can be found inside the .js files of this repo.

Student Learning Objectives

Express

  • Student can apply express.json as top level middle-ware
  • Student can access the body from req.body
  • Student can use post, put, delete methods to handle the associated requests

Top-Level Middleware

  • Student can describe how app.use is fired before every endpoint declared after it.
  • Student can set up express.json in an app.use() at the top of their server file.

Tooling - Postman

  • Student can send requests to their server with Postman.
  • Student can send requests to their server with Postman including a body in the request.
  • Student can send requests to their server with Postman including params in the request.

Express Patterns

  • Student can set up a controller to export content with module.exports and require that controller in their server file.
  • Student can write endpoint handler functions in the controller file and then reference them in the endpoint in the server file.

Top Level Middleware

Top Level Middleware is the term we use to refer to some logic that will be executed upon every request that is made to our API.

We can set up that logic inside of the built method .use() that comes from the object that is return from invoking express.

app.use(/* some function to execute here */);

Req Body

Sometimes we need to send some complex data to our API. Using the req.query or req.params just might not be sufficient enough to use to send this data. This is where the body comes into play.

req.body is the object we can use to receive the complex data. However, the data that comes from the body of the request is received as JSON so we need to parse it into a Javascript Object.

We can do this by using a special function from express called express.json(). This function will parse the JSON that comes as the body from the request and turn it into a useable object for us.

We want to invoke this function as Top Level Middleware so that it is invoked upon every connection made to our API.

We will pass it into our app.use() function to run it as top level middleware.

app.use(express.json());

CORS

CORS stands for Cross Origin Resource Sharing. This is a security protocol that we can use to safely allow other origins talk to our origin. This means that we can make a request from one server to another server.

We first need to install CORS as a dependency for our project. In the terminal, run:

$ npm install cors

Then require it at the top of the index.js file for our API.

const cors = require('cors')

Then we need this function to be invoked as top level middleware.

app.use(cors())

Now your API is setup to follow the CORS protocol.

Postman

Postman is a suite that we can use to act as a client to interact with the API that we build.

The browser, by default, will only let us make get requests from the URL. So we can use postman to test out a full CRUD API.

Full CRUD API

So far we have built an API that has only allowed get requests to be made. We need to also allow post, put, and delete requests to be made to become a full CRUD API.

POST:

app.post('path for the endpoint', handlerFunc)

PUT:

app.put('path for the endpoint', handlerFunc)

DELETE:

app.delete('path for the endpoint', handlerFunc)

We now have a "fully functional" API because we are following the full CRUD pattern.

Controller

A controller is what we can use to store all of our functions that we use to handle the requests being made.

We usually will create a separate javascript file that will hold the controller functions.

Node 2 Additional Resources

Node and Express

Middleware

Postman

Node 2 Mini Project

https://github.com/DevMountain/node-2-mini

Node 2 Afternoon Project

https://github.com/DevMountain/node-2-afternoon

node-2's People

Contributors

matias-perezferrero avatar mattcbodily avatar taytestokes avatar suthyscott avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.