Skip to content

warl0ck769/nutriai-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NutriAI Backend API

Node.js Express MySQL License

REST API backend for the NutriAI mobile application, providing restaurant data, dish information, and AI-powered nutrition estimation for 926,000+ dishes across 9,249 restaurants in the UAE.

Features

  • 🍽️ Restaurant Management - Browse and search 9,249+ restaurants
  • 🥗 Dish Database - Access 926,396 dishes with detailed information
  • 🤖 AI Nutrition Estimation - Groq AI-powered nutrition data estimation
  • 📍 Geolocation - Find nearby restaurants using Haversine formula
  • 🔍 Advanced Search - Filter by cuisine, category, rating, and more
  • 📊 Pagination - Efficient data loading with configurable limits
  • 🌍 CORS Enabled - Ready for mobile app integration

Tech Stack

  • Node.js - Runtime environment
  • Express.js - Web framework
  • MySQL - Database (nutriai_scraped)
  • Axios - HTTP client for Groq AI
  • dotenv - Environment configuration
  • CORS - Cross-origin resource sharing

API Endpoints

See API_DOCUMENTATION.md for complete API reference.

Quick overview:

  • GET /api/v1/restaurants - List restaurants with pagination
  • GET /api/v1/restaurants/nearby - Find restaurants by location
  • GET /api/v1/restaurants/:id - Get restaurant details
  • GET /api/v1/restaurants/:id/menu - Get restaurant menu
  • GET /api/v1/dishes/search - Search dishes
  • GET /api/v1/dishes/:id - Get dish details
  • GET /api/v1/dishes/:id/nutrition - Get nutrition data (with AI estimation)

Database Structure

See DATABASE.md for complete database schema documentation.

The API uses the nutriai_scraped database with 5 main tables:

  • restaurants - Restaurant information (9,249 records)
  • dishes - Dish information (926,396 records)
  • locations - Geographic data
  • restaurant_stats - Performance metrics
  • import_log - Data import tracking

Installation

Prerequisites

  • Node.js 18.x or higher
  • MySQL 8.x or higher
  • npm or yarn

1. Clone Repository

git clone https://github.com/warl0ck769/nutriai-api.git
cd nutriai-api

2. Install Dependencies

npm install

3. Configure Environment

Create a .env file:

# Database Configuration
DB_HOST=your_mysql_host
DB_PORT=3306
DB_USER=your_mysql_user
DB_PASSWORD=your_mysql_password
DB_NAME=nutriai_scraped

# Server Configuration
PORT=3001
NODE_ENV=development

# Groq AI Configuration
GROQ_API_KEY=your_groq_api_key

# CORS Configuration
ALLOWED_ORIGINS=http://localhost:*,https://localhost:*

4. Start Server

# Development
npm start

# Production
npm run start:prod

Server will start on http://localhost:3001

Usage

Basic Request

curl http://localhost:3001/api/v1/restaurants?limit=10

Response Format

{
  "success": true,
  "data": [...],
  "pagination": {
    "total": 9249,
    "limit": 10,
    "offset": 0,
    "has_more": true
  }
}

Project Structure

nutriai-api/
├── src/
│   ├── config/
│   │   └── database.js          # MySQL connection config
│   ├── controllers/
│   │   ├── restaurantController.js  # Restaurant endpoints
│   │   └── dishController.js         # Dish endpoints
│   ├── routes/
│   │   ├── restaurantRoutes.js      # Restaurant routes
│   │   └── dishRoutes.js             # Dish routes
│   ├── services/
│   │   └── nutritionService.js      # Groq AI integration
│   ├── middleware/
│   │   └── cors.js                   # CORS configuration
│   └── server.js                     # Main server file
├── .env                              # Environment variables
├── package.json                      # Dependencies
└── README.md                         # This file

Development

Run in Development Mode

npm run dev

Testing API Endpoints

# Test health check
curl http://localhost:3001/health

# Test restaurant endpoint
curl http://localhost:3001/api/v1/restaurants?limit=5

# Test nearby restaurants
curl "http://localhost:3001/api/v1/restaurants/nearby?lat=25.2048&lng=55.2708&radius=5"

# Test dish search
curl "http://localhost:3001/api/v1/dishes/search?q=chicken&limit=10"

Error Handling

API returns standardized error responses:

{
  "success": false,
  "error": "Error message description"
}

Common HTTP status codes:

  • 200 - Success
  • 400 - Bad Request
  • 404 - Not Found
  • 500 - Internal Server Error

Performance Optimization

  • Connection Pooling - Efficient MySQL connections
  • Pagination - Default 20 items per request
  • Index Optimization - Database indexes on key columns
  • Lazy Loading - Load data on demand
  • Caching - Future implementation planned

Security

  • Environment variables for sensitive data
  • CORS configuration for allowed origins
  • Input validation and sanitization
  • SQL injection prevention with parameterized queries
  • Rate limiting (future implementation)

Deployment

Option 1: Traditional Server

npm install --production
NODE_ENV=production npm start

Option 2: Docker

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 3001
CMD ["npm", "start"]

Option 3: Cloud Platforms

  • AWS EC2 - Traditional deployment
  • Heroku - Easy deployment with MySQL addon
  • DigitalOcean - App Platform or Droplet
  • Railway - Automatic deployments

Environment Variables

Variable Description Required
DB_HOST MySQL host address Yes
DB_PORT MySQL port (default: 3306) Yes
DB_USER MySQL username Yes
DB_PASSWORD MySQL password Yes
DB_NAME Database name (nutriai_scraped) Yes
PORT API server port (default: 3001) No
NODE_ENV Environment (development/production) No
GROQ_API_KEY Groq AI API key Yes for nutrition estimation
ALLOWED_ORIGINS CORS allowed origins No

API Rate Limits

Currently no rate limiting implemented. Recommended for production:

  • 100 requests per minute per IP
  • 1000 requests per hour per IP

Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/AmazingFeature)
  3. Commit changes (git commit -m 'Add AmazingFeature')
  4. Push to branch (git push origin feature/AmazingFeature)
  5. Open Pull Request

Troubleshooting

Database Connection Issues

# Test MySQL connection
mysql -h your_host -u your_user -p nutriai_scraped

# Check if database exists
SHOW DATABASES;
USE nutriai_scraped;
SHOW TABLES;

Port Already in Use

# Find process using port 3001
lsof -i :3001

# Kill process
kill -9 <PID>

Module Not Found

# Reinstall dependencies
rm -rf node_modules package-lock.json
npm install

Monitoring

Recommended tools:

  • PM2 - Process management
  • Morgan - HTTP request logging
  • Winston - Application logging
  • New Relic - Performance monitoring

Roadmap

  • Add authentication and authorization
  • Implement API rate limiting
  • Add caching layer (Redis)
  • WebSocket support for real-time updates
  • GraphQL API option
  • API versioning strategy
  • Comprehensive test suite
  • API analytics and monitoring
  • Swagger/OpenAPI documentation
  • Docker compose setup

Links

License

This project is licensed under the MIT License.

Support

For support, email rahulvashista98@gmail.com or open an issue on GitHub.


Made with ❤️ using Node.js and Express

🤖 Generated with Claude Code

About

NutriAI Backend API - Node.js/Express API with MySQL database for restaurant and nutrition data

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors