1.2 KiB
EXPRESS CALCULATOR
Tonight you'll make a calculator using node and express. It's not very fancy looking. But it will serve as a proof-of-concept for using express and accessing the req.params object.
-
make a directory called
calc -
go into the directory
-
touch
server.js -
npm init -
npm install --save express -
require and set up express at the top of
server.js
- refer to class notes for syntax
-
add a listener for port 3000
-
Make a GET route for
multiplythat has two variable destinations corresponding to two numbers that will be multiplied. The syntax for the route will look like this:
app.get('/multiply/:num1/:num2', function(req, res) {
res.send( // something );
});
Write the code that will multiply the numbers that the user sends through in the url, and respond with the answer.
You will need to access req.params.num1 and req.params.num2 to get it to work.
-
Make another route for add.
-
Make another route for subtract.
-
Make another route for divide.
Running the server
In the directory, run nodemon to run your server.
See the response of your server in your browser at localhost:3000.