Skip to content

brandonmay-dev/facerecognitionbrain-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FaceRecognitionBrain API

A Node.js and Express backend API for the FaceRecognitionBrain full-stack application. This service handles authentication, PostgreSQL persistence, secure password hashing, user image-submission tracking, and server-side Clarifai face-detection requests.


Why This Project Stands Out

This backend demonstrates practical server-side development beyond basic CRUD operations.

It supports a real React frontend, manages user data securely, communicates with a PostgreSQL database, protects third-party API credentials on the server, and acts as the bridge between the client and Clarifai’s AI face-detection model.


Key Highlights

  • REST API built with Node.js and Express
  • User registration and sign-in flow
  • Password hashing with bcryptjs
  • PostgreSQL persistence with Knex
  • Server-side Clarifai API integration
  • User-specific image submission tracking
  • Environment-based configuration for local and deployed environments
  • CORS handling for frontend/backend communication

What The API Does

  • Registers new users with hashed passwords
  • Authenticates returning users
  • Stores and retrieves user profile data
  • Sends submitted image URLs to Clarifai’s face-detection model
  • Returns AI detection data to the frontend
  • Increments each user’s image-processing count
  • Provides health-check endpoints for backend verification

Tech Stack

  • Node.js – JavaScript runtime for backend development
  • Express – API routing and request handling
  • PostgreSQL – relational database for users and login data
  • Knex – SQL query builder for database operations
  • bcryptjs – password hashing and verification
  • dotenv – environment variable management
  • cors – frontend/backend request handling

Architecture Overview

  1. React frontend sends authentication or image-processing requests to the API
  2. Express receives the request and routes it to the appropriate handler
  3. Knex reads from or writes to PostgreSQL
  4. Passwords are hashed and verified with bcryptjs
  5. Image URLs are sent from the backend to Clarifai using a private API key
  6. API returns user data, Clarifai response data, or updated entry counts to the frontend

API Endpoints

GET /

Health check endpoint for confirming the server is running.

Example response:

{
  "ok": true
}

GET /health/whoami

Returns information about the connected PostgreSQL database and current database user.

Useful for verifying database connectivity in deployed environments.


POST /register

Creates a new user and stores a hashed password.

Example request:

{
  "name": "Jane Doe",
  "email": "jane@example.com",
  "password": "supersecret"
}

POST /signin

Authenticates an existing user by comparing the submitted password against the stored password hash.

Example request:

{
  "email": "jane@example.com",
  "password": "supersecret"
}

POST /imageurl

Accepts a public image URL and forwards it to Clarifai’s face-detection model.

The Clarifai API key is stored on the backend so it is never exposed in the frontend.

Example request:

{
  "input": "https://example.com/face.jpg"
}

PUT /image

Increments the signed-in user’s image submission count.

Example request:

{
  "id": 1
}

GET /profile/:id

Returns the stored profile data for a specific user.


Environment Variables

Create a .env file in the project root.

Local PostgreSQL Setup

CLARIFAI_PAT=your_clarifai_pat
DB_HOST=127.0.0.1
DB_PORT=5432
DB_NAME=smart-brain
DB_USER=your_postgres_user
DB_PASSWORD=your_postgres_password
PORT=3001

Deployment Setup

CLARIFAI_PAT=your_clarifai_pat
DATABASE_URL=your_database_url
PORT=3001

Database Setup

Make sure PostgreSQL is running, then create the database:

CREATE DATABASE smart-brain;

This API expects login and users tables.

login Table

CREATE TABLE login (
  id serial PRIMARY KEY,
  hash varchar(100) NOT NULL,
  email text UNIQUE NOT NULL
);

users Table

CREATE TABLE users (
  id serial PRIMARY KEY,
  name varchar(100),
  email text UNIQUE NOT NULL,
  hash varchar(100) NOT NULL,
  entries bigint DEFAULT 0,
  joined timestamp NOT NULL
);

Local Development

Install dependencies:

npm install

Run in development mode:

npm run dev

Run in production mode:

npm start

The API runs locally at:

http://localhost:3001

Deployment Notes

  • DATABASE_URL is used automatically when present
  • SSL is enabled for hosted PostgreSQL connections
  • Clarifai credentials are kept on the server, not the frontend
  • CORS is configured to control which frontend origins can access the API
  • If the deployed frontend URL changes, update the backend CORS allowlist in server.js

What This Project Demonstrates

  • Building a REST API for a separate frontend client
  • Implementing authentication with hashed passwords
  • Working with relational data using PostgreSQL and Knex
  • Protecting third-party API secrets on the server
  • Integrating external AI services into backend workflows
  • Managing local and production environment configuration
  • Supporting deployment-specific database behavior

Future Improvements

  • Add database migrations and seed files
  • Add validation middleware for request bodies
  • Add automated route and integration tests
  • Move CORS allowlist values into environment variables
  • Split server.js into routes, controllers, and services
  • Add centralized error handling
  • Add logging and rate limiting
  • Add token-based authentication with JWT or sessions

Author

Brandon May


License

MIT

About

Backend API for a full-stack AI face detection app, built with Node.js, Express, PostgreSQL, and Clarifai.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors