Build an Express.js API that converts Celsius to Fahrenheit.
- Route:
GET /convert - Query Parameter:
celsius(number) - Returns: JSON with both temperatures
GET /convert?celsius=25
{
"celsius": 25,
"fahrenheit": 77
}Return proper error for:
- Missing
celsiusparameter - Invalid input (not a number)
{
"error": "Invalid input: celsius must be a number"
}- Open
server.js - Find the
TODOsections - Implement the conversion logic
- Test your API
node server.jsServer runs on port 3000.
# Test valid request
curl "http://localhost:3000/convert?celsius=0"
# Test invalid request
curl "http://localhost:3000/convert?celsius=abc"
# Or run automated tests
node test.jsFahrenheit = (Celsius × 9/5) + 32
- Check if
celsiusquery parameter exists - Validate it's a number using
isNaN() - Use
parseFloat()to convert string to number - Round to 2 decimal places
Good luck! 🚀