DEV Community

Jesse Ekoh-Ordan
Jesse Ekoh-Ordan

Posted on

HNG13: My First Steps

The 13th cohort began yesterday, and it's already been hectic. From joining the official Slack workspace (I hate group chat platforms, looking at you, Discord 😒), reading instructions, and interacting with various people, it has been noisy trying to follow conversations happening in the various channels I'm in, but I'll figure it out.
For my first task, I was instructed to create a Profile Endpoint that would respond with my name, email, tech stack, and a random cat fact. Although this was going to be light work, I still had issues clarifying the instruction on what the response was going to be if the request to the external cat fact API CatFact.ninja fails. I asked my questions in the help channels and some of my nice colleagues came to my aid and put me through.

my project files

For my implementation, I chose to use ExpressJS with TypeScript. I imported Express and created a new Express app. I ensured that I installed other required middleware to follow Express best practices, such as CORS and express-rate-limit. I also set up logging using Pino. Why Pino? 🤷‍♂️ I came across an article about it being faster than alternatives like Winston and Morgan, so I've been defaulting to Pino ever since.

image of app.ts

After setting up all the middleware, I created the route handle for the /me route. In the handler, I fetched a cat from an external API, and if it is successful, I return a response in this format

{
  "status": "success",
  "user": {
    "email": "<my email>",
    "name": "<my full name>",
    "stack": "<my backend stack>"
  },
  "timestamp": "<current UTC time in ISO 8601 format>",
  "fact": "<random cat fact from Cat Facts API>"
}
Enter fullscreen mode Exit fullscreen mode

and if the cat API times out or fails, I still respond with the same format. The only difference is that the cat fact becomes a default string.

app.get("/me", async (req, res) => {
  const now = new Date();
  const user = {
    email: "Jesseekoh@outlook.com",
    name: "Jesse Ekoh-Ordan",
    stack: "Node.js/Express",
  };

  let catFact: string;
  try {
    const response = await axios.get("https://catfact.ninja/fact", {
      timeout: 5000,
    });

    catFact = response.data.fact;
  } catch (error) {
    logger.warn(error, "Cat Facts API error");
    catFact =
      "Cats' hearing stops at 65 khz (kilohertz); humans' hearing stops at 20 khz.";
  }

  res.status(200).json({
    status: "success",
    user,
    timestamp: now.toISOString(),
    fact: catFact,
  });
});
Enter fullscreen mode Exit fullscreen mode

Image of index.ts

The only hiccups I experienced completing this task were with deployment, as I was instructed not to use Vercel or Render. I tried using Railway but could not deploy because I had exhausted my free trial. I also tried Pxxl.app, but it just wouldn't deploy. I then created a new Railway account using my other email address and was finally able to deploy.

I'm really excited for what's to come in HNG

Top comments (0)