Giter VIP home page Giter VIP logo

express-routes-calc's Introduction

GA Logo Express Routes Calculator ๐Ÿงฎ

Your task is to create a webserver calculator using Express routes. Your calculator should handle GET (app.get()) requests to /add, /subtract, /multiply and /divide. You should use URL parameters to handle requests to your calculator.

Example using URL parameters:

route response
/add/:x/:y responds with x + y
/subtract/:x/:y responds with x - y
/multiply/:x/:y responds with x * y
/divide/:x/:y responds with x / y

This is an example of a simple GET route with request parameters using express:

app.get('/:input', (req, res) => {
  console.log("req.params: ", req.params)
  res.send("Our parameter is " + req.params.input + ".");
})

Getting Started

  • fork and clone this repo and then cd in into express-routes-calc
  • run npm init to initialize a node project
  • run npm i express to install the required packages
  • touch index.js to create the main entry point for your express app
  • use nodemon to run your express app

Before writing your routes, set up a simple express app with a hello world route to test that everything is working.

HELP! I'm stuck setting up an express app!

Check out this pseudocode, it might help:

// import required modules

// configure express app

// define routes

// listen on a port
Stop Messing Around! I'm really stuck!!

This is an example of the most simple express server:

// import required modules
const express = require('express')

// configure express app
const app = express()
const PORT = 3000

// define routes
app.get('/', (req, res) => {
  res.send('Hello, World!')
});

// listen on a port
app.listen(PORT, () => {
  console.log(`listening to the smooth sounds of port ${PORT} in the morning ๐ŸŒŠ`)
})

Bonus

Make it so your calculator can accept any amount of numbers to do math with instead of just 2 hardcoded ones with the wildcard * route variable. Instead if having the the math operation as the first route parameter, send it in the query params, so one route can handle addition, subtraction, multiplication, and division.

For example the URL /5/6/7/8?math=add would respond with 26 and the URL 5/6/7/8?math=subtract would respond with -16. Likewise 5/6/7/8/9/10?math=add should respond with 45 and so on.

Use this example at /* to experiment with how this might work:

app.get("/*", (req, res) => {
  console.log(req.params)
  res.send(req.params)
})

Follow along with the gitbook notes on express routing for some tips and tricks.


Licensing

  1. All content is licensed under a CC-BY-NC-SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

express-routes-calc's People

Contributors

weston-bailey 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.