Skip to content

Instantly share code, notes, and snippets.

@akshajmody
Created May 2, 2020 04:28
Show Gist options
  • Save akshajmody/338cef1a1094d140716a5a5f7259c025 to your computer and use it in GitHub Desktop.
Save akshajmody/338cef1a1094d140716a5a5f7259c025 to your computer and use it in GitHub Desktop.
Sample API call successfully retrieves all DB data. Next step is retrieving by ID
//ROUTER
const router = require('express').Router();
const controller = require('./controller.js');
router.get('/findAll', controller.get);
module.exports = router;
//CONTROLLER
const path = require('path');
const findAllProducts = require(path.join(__dirname, 'db', 'productsdb.js')).findAllProducts;
let controller = {
get: (req, res) => {
findAllProducts((err, results) => {
if (err) {
console.log(err);
res.sendStatus(500);
} else {
res.json(results);
}
});
},
};
module.exports = controller;
//OUTPUT - SHORT SAMPLE OF OUTPUT OF ALL DATA
[{
"zip": [
94747,
94687,
94166,
94383,
94648,
94491,
94203,
94325,
94149,
94596,
94017,
94152,
94374,
94964,
94268,
94656,
94167,
94804,
94109
],
"_id": "5eace4c4d5c7ef4196fe17a0",
"recommended": [
{
"_id": "5eace4c4d5c7ef4196fe17a1",
"name": "Cargo Fire Pants",
"price": 48,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+1.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17a2",
"name": "Utilitarian NyCo Pants",
"price": 50.67,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+1.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17a3",
"name": "Futuristic Heat-tech Pants",
"price": 17.18,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+3.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17a4",
"name": "Parachute NyCo Pants",
"price": 24,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+5.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17a5",
"name": "Technical Cargo Goretex Pants",
"price": 70.08,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+2.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17a6",
"name": "Multi-purpose Goretex Pants",
"price": 43.69,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+6.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17a7",
"name": "Cargo Heat-tech Pants",
"price": 31.92,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+8.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17a8",
"name": "Utilitarian NyCo Pants",
"price": 61.75,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+6.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17a9",
"name": "Cargo Goretex Pants",
"price": 43.67,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+8.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17aa",
"name": "Utility NyCo Pants",
"price": 3.44,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+5.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17ab",
"name": "Technical Cargo Heat-tech Pants",
"price": 20.48,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+1.jpg"
}
],
"productId": 4,
"price": 72.02,
"image": "https://targetrecombucket.s3-us-west-1.amazonaws.com/PANTS+2.jpg",
"__v": 0
},
{
"zip": [
94920,
94676,
94444,
94997,
94867,
94693,
94289,
94766,
94587,
94626,
94046,
94244,
94729,
94997,
94070,
94906,
94632,
94874,
94083,
94163,
94467,
94857,
94308,
94850,
94021,
94205,
94524,
94510,
94858,
94434,
94409,
94834,
94894,
94470,
94812,
94576,
94464,
94226,
94671,
94557,
94518,
94615,
94102,
94045,
94724,
94125,
94212,
94008,
94734
],
"_id": "5eace4c4d5c7ef4196fe17ac",
"recommended": [
{
"_id": "5eace4c4d5c7ef4196fe17ad",
"name": "Silver Anti-Bacterial Cotton Noragi",
"price": 35.22,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/NORAGI+1.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17ae",
"name": "Technical Jersey Noragi",
"price": 35.45,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/NORAGI+4.jpg"
},
{
"_id": "5eace4c4d5c7ef4196fe17af",
"name": "Functional Ripstop Noragi",
"price": 48.93,
"recImage": "https://targetrecombucket.s3-us-west-1.amazonaws.com/NORAGI+6.jpg"
}
],
"productId": 5,
"price": 67.68,
"image": "https://targetrecombucket.s3-us-west-1.amazonaws.com/NORAGI+3.jpg",
"__v": 0
}]
@akshajmody
Copy link
Author

APIcalls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment