DEV Community

Faruq Awe
Faruq Awe

Posted on

Building a RESTful API with Node.js and Cat Facts πŸš€

Node.js API

🎯 The Challenge

As part of my backend development stage at HNG, I recently built a simple RESTful API that combines personal profile information with dynamic cat facts. Here's my story of turning requirements into a fully deployed application.

πŸ“‹ Task Requirements

The goal was straightforward but comprehensive:

  • Create a GET endpoint at /me
  • Return JSON with specific structure
  • Integrate with external Cat Facts API
  • Include dynamic UTC timestamp
  • Handle errors gracefully
  • Deploy to a cloud platform

πŸ› οΈ Tech Stack

  • Backend: Node.js with Express
  • HTTP Client: Axios for external API calls
  • Deployment: Render.com
  • Environment Management: dotenv

πŸš€ The Implementation Journey

Step 1: Setting Up the Foundation

I started by creating a basic Express server with proper middleware:

Step 2: External API Integration

The most exciting part was integrating with the Cat Facts API:

Step 3: Building the Main Endpoint

The core GET /me endpoint brought everything together:

Challenges

Challenge 1: CORS Package Compatibility
Issue: Path-to-regexp errors with Node.js v22
Solution: Implemented manual CORS headers instead of using the cors package

Challenge 2: Deployment Platform Issues
Experience: Tried PXXL.app initially, encountered "no available server" errors
Solution: Successfully deployed on Render.com with their generous free tier

πŸ“Š The Final Result

API Response Structure

πŸ”— Project Links

πŸ“š Resources That Helped

Building this API taught me that even simple projects involve complex considerations. From handling time zones to managing external API dependencies, every line of code serves a purpose.
P.S. Don't forget to test the live API and let me know what cat fact you get!

Top comments (0)