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.
- 👉 Frontend Repo: facerecognitionbrain
- 👉 Backend Repo: facerecognitionbrain-api
- 👉 Live API: safe-dawn-54877-2bdeb01ab080.herokuapp.com
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.
- 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
- 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
- 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
- React frontend sends authentication or image-processing requests to the API
- Express receives the request and routes it to the appropriate handler
- Knex reads from or writes to PostgreSQL
- Passwords are hashed and verified with
bcryptjs - Image URLs are sent from the backend to Clarifai using a private API key
- API returns user data, Clarifai response data, or updated entry counts to the frontend
Health check endpoint for confirming the server is running.
Example response:
{
"ok": true
}Returns information about the connected PostgreSQL database and current database user.
Useful for verifying database connectivity in deployed environments.
Creates a new user and stores a hashed password.
Example request:
{
"name": "Jane Doe",
"email": "jane@example.com",
"password": "supersecret"
}Authenticates an existing user by comparing the submitted password against the stored password hash.
Example request:
{
"email": "jane@example.com",
"password": "supersecret"
}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"
}Increments the signed-in user’s image submission count.
Example request:
{
"id": 1
}Returns the stored profile data for a specific user.
Create a .env file in the project root.
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=3001CLARIFAI_PAT=your_clarifai_pat
DATABASE_URL=your_database_url
PORT=3001Make sure PostgreSQL is running, then create the database:
CREATE DATABASE smart-brain;This API expects login and users tables.
CREATE TABLE login (
id serial PRIMARY KEY,
hash varchar(100) NOT NULL,
email text UNIQUE NOT NULL
);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
);Install dependencies:
npm installRun in development mode:
npm run devRun in production mode:
npm startThe API runs locally at:
http://localhost:3001DATABASE_URLis 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
- 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
- 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.jsinto routes, controllers, and services - Add centralized error handling
- Add logging and rate limiting
- Add token-based authentication with JWT or sessions
Brandon May
MIT