Giter Site home page Giter Site logo

pursuit-core-web-express-routing-lab's Introduction

Express-Routing-Lab

In this lab you will get to build a small server that can serve us as calculator for simple math operations.

1. Calculator API - Four Routes

Backend

In a file calculatorApi1.js, create and express server that works as a basic calculator API.

  • Create four routes/endpoints, /add, /sub, /mul, /div. Each of the routes will then accept two numbers as route parameters.

    Here is what a route in your app would look like: /add/:num1/:num2

  • In each route, convert the numbers to integers using parseInt.

  • Respond to each request with an object that contains both numbers and the result.

    For example, if the users navigates to http://localhost:8000/add/2/4, the response should be JSON like:

    {"num1": 2, "num2": 4, "result": 6}

  • If either of the parameters is not a number, respond with an string like "numbers only".

  • If the user navigates to any other route, respond with an explanatory message about your API.

Frontend

Build a frontend where a user can interact with your calculator server. It should look something like this:

calculatorGif.gif

2. Calculator API - One Route

In a file calculatorApi2.js, convert your calculator API to take the operator as a route parameter as well. Now you will only need one route. If the operator is not valid, send an appropriate response like "the operator is not supported".

Hints
  • Since you will have only one route or endpoint what would that be?
  • This calculator server/API should work exactly the same as your previous for the basic operations. But you should see that your code is much shorter.
  • How could you make the operator portion of the URL be a parameter itself? So that it can be captured/seen in req.params

3. Challenge - Base Conversion API

Create an express server that converts numbers between three bases: decimal, binary, and hexadecimal.

  • To convert a decimal number to binary and hex:
let num = 10;
num.toString(2) // '1010'
num.toString(16) // 'a'
  • To convert binary and hex strings to decimal:
let hexStr = 'a'
parseInt(hexStr, 16) // 10
let binaryStr = '1010'
parseInt(binaryStr, 2) // 10
  • On every request, the server will expect a url of the following structure:

localhost:8000/{number}/{bin|dec|hex}

It will return an object that contains {number} and all conversions for {number} with the provided base.

  • If only a number is provided, e.g. localhost:8000/{number} - assume {number} has a decimal base and return an object with all conversions.

Example 1: on url localhost:8000/10 the server should return:

{
  "original": { "value":"10", "base":10 },
  "conversions": { "decimal":"10", "binary":"1010", "hex":"a" }
}

Example 2: on url localhost:8000/11/bin the server should return

{
  "original": { "value": "11", "base":2 },
  "conversions": { "decimal": "3", "binary": "11", "hex":" 3" }
}

pursuit-core-web-express-routing-lab's People

Contributors

alejo4373 avatar benstone1 avatar

Watchers

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