Skip to content

Instantly share code, notes, and snippets.

@mariomui
Last active December 22, 2018 00:16
Show Gist options
  • Save mariomui/98dcd729a8a300ce56c484d249f2c87f to your computer and use it in GitHub Desktop.
Save mariomui/98dcd729a8a300ce56c484d249f2c87f to your computer and use it in GitHub Desktop.
Example API call
// --------------------------------------------- SERVER CODE --------------------------------------------- //
app.get('/rooms/:id/pictures', function (req, res) {
  console.log('hello');
  findPicturesByRoomId(req.params.id)
    .then(function (images) {
      res.send(images.image_url);
    });
});

app.get('/:id', function (req, res) {
  res.sendFile(path.resolve(__dirname, '../public/index.html'));
});
// -------------------------------------------- DATABASE CODE -------------------------------------------- //
const findPicturesByRoomId = (roomNumber) => {
  return PicturesModel.findOne({ room_id: roomNumber }).then((data) => {
    return data;
  })
}

// ---------------------------------------- POSTMAN GET REQUEST URL ---------------------------------------- //
localhost:4500/rooms/1/pictures
// ------------------------------------------ GET REQUEST RESPONSE ------------------------------------------ //
[
    "http://lorempixel.com/640/480",
    "http://lorempixel.com/640/480",
    "http://lorempixel.com/640/480",
    "http://lorempixel.com/640/480",
    "http://lorempixel.com/640/480"
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment