Giter Site home page Giter Site logo

node-challenge-calculator's Introduction

node-challenge-calculator

Make a calculator adding an endpoint for each operation [Add,Substract,Multiply,Divide], each endpoint will receive two numbers and return the result

Step 0: Build a node server from scratch with 'npm init'

[1] First create a directory named myapp to hold your application, and make that your working directory:

$ mkdir myapp
$ cd myapp

[2] Make a package.json file. The package.json file is easy to create from the command line. Type the following command into your terminal to get started:

$ npm init

This command will initialise a step-by-step process for creating the package.json. It will ask you a bunch of questions.

You can skip most of the questions but change the entry point from (index.js) to server.js.

The wizard asks you for the following information: name, version, description, main, test, repository, keywords, author, license - do you understand all of them?

At the endo of the wizard, you should see a new file called package.json in your project's folder.

Here is an example package.json file for a project called Passport.

[3] Before we write any code, you'll need to install the Express library.

As we install Express, we'll need to update the package.json to add Express as a dependency. We do this so that other people working on the project will know to install Express before running any of the code. This can be done by adding --save to the end of your command.

Run the following command in your terminal:

npm install express --save

Express should now be installed. Check your package.json file to make sure it has been added as a dependency. It will look like this:

package.json screenshot

[4] In the myapp directory, create a file named server.js and copy in the code from the example above.

[5] Run the app with the following command:

$ node server.js

[6] Then, load http://localhost:3000/ in a browser to see the output of the browser and the terminal

Step 1: Reading endpoint query

Example when you ask for a video on youtube: https://www.youtube.com/watch?v=7UQBMb8ZpuE

An idea of how do we read the query in Node:

app.get('/watch', (req, res) => {
    const video = req.query.v;
    console.log("The user request the video ID " + video);
    res.send('The user request the video ID  ' + video);
}) 

Build the next endpoints of our calculator:

NOTE: put a comment if you have to do .toString to return the result

Step 2: Reading endpoints parameters

Example when you ask a repository to Github:

An idea of how do we read the parameters in Node:

app.get('/:account/:repository', (req, res) => {
    const accountID = req.params.account;
    const repositoryID = req.params.repository;
    console.log("The user requested the repository" + repositoryID + " of the user " + accountID);
    res.send('The user requested the repository' + repositoryID + ' of the user ' + accountID)
}) 

Build the next endpoints of our calculator:

Step 3: use a logger

Adding this logger to your server, will log in the console all the requests

const myLogger = (req, res, next) => {
  const visitTime = new Date();
  console.log(`visited ${req.url} at ${visitTime.toLocaleString()}`);
  next();
};
app.use(myLogger);

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.