Skip to content

Instantly share code, notes, and snippets.

@shubhamp-sf
Created May 3, 2024 08:25
Show Gist options
  • Save shubhamp-sf/0af7c6b46482f6787b730f3197cec874 to your computer and use it in GitHub Desktop.
Save shubhamp-sf/0af7c6b46482f6787b730f3197cec874 to your computer and use it in GitHub Desktop.
Interview Challenge
const http = require("http");
const url = require("url");
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const path = parsedUrl.pathname;
const query = parsedUrl.query;
const numberOfImagesRequest = query.count;
if (path === "/images") {
if (req.method === "GET") {
console.time("/images");
const imageUrls = [];
// Write your code here...
console.timeEnd("/images");
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify(imageUrls));
} else {
res.statusCode = 405;
res.setHeader("Allow", "GET");
res.end("Method Not Allowed");
}
} else {
res.statusCode = 404;
res.end("Not Found");
}
});
const port = 3000;
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment