Giter Club home page Giter Club logo

api_routes_exercise_exercise_2's Introduction

Backend Api Routes: Random Character

In this challenge, you will

  • create an api route which returns a character object with random values using the chance package and
  • consume this endpoint with SWR to render the information in the browser.

Task

Create the API

Create an api route which returns a character object with random values using chance.

You can use the following hints as guideline:

  • Create the file structure pages/api/random-character.js.
  • Switch to pages/api/random-character.js; write a handler function which
    • responds with a 200 status code and
    • with a character object containing random information (see example below);
    • parses the character object with the .json() method.
  • To create random values, use chance:
    • import via import Chance from "chance";
    • create a new instance with const chance = new Chance();
    • create a character object and use the methods of chance like in the example below;
    • feel free to add keys as you wish: every character should have a twitter name and a geohash, right?
// example character object with random values
const character = {
  firstName: chance.first(),
  lastName: chance.last(),
  // ... some more keys here ...
};

Check your api route:

  • run npm run dev and
  • switch to the browser and open /api/random-character: you should now see the keys of the character object with random values.

Create the Frontend

You have now prepared an api route responding with a random character โ€“ let's use it to display the data for the user!

  • Switch to pages/index.js;
  • Import the useSWR hook, write a fetcher for it and fetch the route /api/random-character.
  • Adapt the return statement to display the data on screen.
  • Open the browser: the home page under / should now show your fetched data!

Notes

  • You only have to touch the ./pages/index.js file.

Development

Local Development

To work locally, please install the dependencies using npm i first.

Run npm run dev to start a development server and open the displayed URL in a browser.

Use npm run test to run the tests.

CodeSandbox Cloud

Select the "Preview: 3000" tab to view this project.

Select the "Tests: logs" tab to view the tests.

The npm run dev and npm run test scripts run automatically.

Scripts

You can use the following commands:

  • npm run dev to start a development server
  • npm run build to build the project
  • npm run start to start a production server
  • npm run test to run the tests
  • npm run lint to run the linter

api_routes_exercise_exercise_2's People

Contributors

hannesoster avatar

Watchers

 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.