Skip to content

๐Ÿ› ๏ธ 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.


Letโ€™s set up your workspace:

Terminal window
mkdir openai-backend
cd openai-backend
npm init -y

Create your environment file:

Terminal window
touch .env
echo "node_modules/" > .gitignore
echo ".env" >> .gitignore

What just happened? You created a Node project and protected your secrets from being committed to Git.


Core packages:

Terminal window
npm install express cors dotenv openai

Development tools:

Terminal window
npm install -D nodemon

What each package does:

  • express - Your web server (handles HTTP requests)
  • cors - Lets your frontend talk to your backend
  • dotenv - Loads your API keys securely
  • openai - Official OpenAI JavaScript client
  • nodemon - Auto-restarts server when you make changes

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!


Open .env and add your OpenAI API key:

OPENAI_API_KEY=sk-proj-your-actual-key-here
PORT=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.


Create index.js:

import express from "express";
import { config } from "dotenv";
import cors from "cors";
// Load environment variables
config();
const app = express();
const PORT = process.env.PORT || 8000;
// Middleware
app.use(cors()); // Allow frontend connections
app.use(express.json()); // Parse JSON requests
// Test route
app.get("/", (req, res) => {
res.json({
message: "๐Ÿค– OpenAI Backend is running!",
status: "ready"
});
});
// Start server
app.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

Start your server:

Terminal window
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:

Terminal window
curl http://localhost:8000

โ€œCannot use import statementโ€

  • โœ… Add "type": "module" to your package.json

โ€œPort 8000 already in useโ€

  • โœ… Change PORT=8001 in your .env file

โ€œcommand not found: npmโ€

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 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!