-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
27 lines (23 loc) · 850 Bytes
/
index.js
File metadata and controls
27 lines (23 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const express = require('express');
const app = express();
app.get('/', function(req, res) {
res.send('Hello World');
});
app.get('/api/products', function(req, res) {
res.send('Display all the products');
});
//executed when Add-To-Cart button for a product is clicked
app.post('/api/products/addtocart/:id', function(req, res) {
/*
:id is used because each product will have its own unique id.
So, instead of making a route for each id, what we can do is use
:id at the end. We can now access the id that is passed on by:
req.params.id
So, if the url is 'localhost:2345/api/products/addtocart/5' then
req.params.id will have a value equal to 5
*/
res.send('Product with id: ' + req.params.id + ' added to cart');
});
app.listen(2345, function() {
console.log('Listening at localhost:2345');
})