Coder Social home page Coder Social logo

ds-errors-and-cors's Introduction

DataServer - Exercise

Errors, arise! But we will keep you in check...

You reached the fuckup workshop.

We will produce some errors now. Something that you were probably quite good at so far... right?

  • Setup a 404 middleware

    • It should return you 404 + { error: `Route ${req.url} does not exist }
    • Test in the browser to call an unknown route, e.g. /idontknow
    • Works? You get the error? Good. Let's move on...
  • Setup a GET route /fuckup/saw-it-coming:

    • Produce here an error handled by try & catch
    • call the next function in your catch block
    • pass in the error object as a parameter to the next function
  • Setup a GET route /fuckup/surprise:

    • Produce here an unhandled error with throw new Error('<your error message>')
  • Test the routes and see what you get

    • HTML? We don't want that... our frontend expects always JSON from us. That is the convention
  • Setup JSON responses for all your errors now

    • Setup a generic error handler: app.use((err, req, res, next) => ...
    • Return the error message you receive here as JSON object
      • that error handler will from now on be responsible to receive and format all our errors, so we have a unified error handling
  • Adapt your 404 route:

    • instead of handling the 404 error here directly
      • forward the error to your new, generic error handler
      • use the next function for that!
  • Test the two routes again

    • All your routes should return JSON errors now

You did that?

Congratulations!

We now completed the essentials of ExpressJS.

You just learned your very first backend framework.

Bonus - Get over the CORS mountain...

Setup a React app. Install it into a folder "client" (npx create-react-app client)

Step into the App.js. Place a button and onClick: Make a call to your POST route.

Start React and the backend in two separate terminals.

Open React in the browser. Click the button to make your POST call.

Check the browser console for a CORS error.

Setup CORS middleware in your backend to make the request work.

Redo your button click in React. Works??

ds-errors-and-cors's People

Contributors

losrobbos avatar

Watchers

 avatar  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.