const axios = require("axios");
const express = require("express");
app.use(express.static("public"));
if (!process.env.PIXABAY_API_KEY) {
throw new Error("The PIXABAY_API_KEY environment variable is not set");
app.post("/content/resources/find", async (request, response) => {
// Handle "CONTAINER" requests
if (request.body.types.includes("CONTAINER")) {
// The user has opened a container
if (request.body.containerId) {
// The user has not opened a container
if (!request.body.containerId) {
// Handle "IMAGE" requests
if (request.body.types.includes("IMAGE")) {
url: "https://pixabay.com/api/",
key: process.env.PIXABAY_API_KEY,
// The user has opened a container
if (request.body.containerId) {
options.params.category = request.body.containerId;
const pixabay = await axios.request(options);
// Transform the array of images into an array of "IMAGE" resources
const images = pixabay.data.hits.map((image) => {
name: `Photo by ${image.user}`,
height: image.webformatHeight,
width: image.webformatWidth,
contentType: "image/jpeg",
// Provide a success response
app.listen(process.env.PORT || 3000);