๐ ๏ธ Node.js Backend Setup
Time to build your AI backend! ๐
Weโre using Node.js + Express because itโs simple, reliable, and works with any frontend (React, Vue, mobile apps, browser extensions). Plus, you have full control over your server.
Why not Next.js? Serverless functions can be tricky with file uploads and long-running AI processes. A standalone backend keeps things predictable.
๐ Step 1: Create Your Project
Section titled โ๐ Step 1: Create Your ProjectโLetโs set up your workspace:
mkdir openai-backendcd openai-backendnpm init -y
Create your environment file:
touch .envecho "node_modules/" > .gitignoreecho ".env" >> .gitignore
What just happened? You created a Node project and protected your secrets from being committed to Git.
๐ฆ Step 2: Install Dependencies
Section titled โ๐ฆ Step 2: Install DependenciesโCore packages:
npm install express cors dotenv openai
Development tools:
npm install -D nodemon
What each package does:
express
- Your web server (handles HTTP requests)cors
- Lets your frontend talk to your backenddotenv
- Loads your API keys securelyopenai
- Official OpenAI JavaScript clientnodemon
- Auto-restarts server when you make changes
โ๏ธ Step 3: Configure Modern JavaScript
Section titled โโ๏ธ Step 3: Configure Modern JavaScriptโUpdate your package.json
to enable modern JavaScript:
{ "name": "openai-backend", "version": "1.0.0", "type": "module", "scripts": { "start": "node index.js", "dev": "nodemon index.js" }, "dependencies": { // your packages will be here }}
Key change: "type": "module"
lets you use import
instead of require()
. Much cleaner!
๐ Step 4: Add Your API Key
Section titled โ๐ Step 4: Add Your API KeyโOpen .env
and add your OpenAI API key:
OPENAI_API_KEY=sk-proj-your-actual-key-herePORT=8000
Replace sk-proj-your-actual-key-here
with your real API key from the previous lesson.
Security reminder: Never commit .env
to Git. Your .gitignore
already protects you.
๐ ๏ธ Step 5: Create Your Server
Section titled โ๐ ๏ธ Step 5: Create Your ServerโCreate index.js
:
import express from "express";import { config } from "dotenv";import cors from "cors";
// Load environment variablesconfig();
const app = express();const PORT = process.env.PORT || 8000;
// Middlewareapp.use(cors()); // Allow frontend connectionsapp.use(express.json()); // Parse JSON requests
// Test routeapp.get("/", (req, res) => { res.json({ message: "๐ค OpenAI Backend is running!", status: "ready" });});
// Start serverapp.listen(PORT, () => { console.log(`๐ Server running on http://localhost:${PORT}`); console.log(`๐ Ready for OpenAI integration!`);});
What this code does:
- Creates an Express server on port 8000
- Enables CORS so your frontend can connect
- Parses JSON requests automatically
- Provides a test endpoint to verify everything works
๐งช Step 6: Test Your Server
Section titled โ๐งช Step 6: Test Your ServerโStart your server:
npm run dev
You should see:
๐ Server running on http://localhost:8000๐ Ready for OpenAI integration!
Test in your browser: Visit http://localhost:8000
Success looks like:
{ "message": "๐ค OpenAI Backend is running!", "status": "ready"}
Test with curl:
curl http://localhost:8000
๐ง Troubleshooting Common Issues
Section titled โ๐ง Troubleshooting Common IssuesโโCannot use import statementโ
- โ
Add
"type": "module"
to yourpackage.json
โPort 8000 already in useโ
- โ
Change
PORT=8001
in your.env
file
โcommand not found: npmโ
- โ Install Node.js from nodejs.org
Server starts but canโt connect
- โ Check your firewall settings
- โ
Try
http://127.0.0.1:8000
instead
โCannot find module โexpressโโ
- โ
Run
npm install
again - โ Make sure youโre in the right directory
๐ Your Project Structure
Section titled โ๐ Your Project StructureโYour backend should look like this:
openai-backend/โโโ index.js # Your main server fileโโโ package.json # Project configurationโโโ .env # API keys (never commit!)โโโ .gitignore # Protects secretsโโโ node_modules/ # Dependencies (auto-generated)
Perfect! Your backend is ready for AI integration. ๐
Whatโs next? Weโll connect to OpenAI and create your first AI endpoint. Time to make your server intelligent!
๐ Next: Your First AI Endpoint - Letโs add some AI magic!