Skip to content

๐Ÿ› ๏ธ Node.js Backend Setup

Time to build your AI backend! ๐Ÿš€

Weโ€™ll use Node.js with Express. Itโ€™s straightforward, reliable, and works with any frontendโ€”React, Vue, mobile apps, or browser extensions. This setup gives you full control over your server and is easy to adapt for production.


Next.js is great for building websites and small APIs. But for AI apps, especially those running in production, a dedicated backend can save you a lot of headaches.

Hereโ€™s why:

  • Serverless timeouts: Large AI jobs (like processing big prompts or files) can exceed serverless limits.
  • Real-time features: Audio, video, and long-running AI tasks work better with a traditional backend.

๐Ÿ’ก You can always switch to Next.js later if you want its frontend features, but starting with Express keeps things simple and reliable.


Letโ€™s set up your workspace:

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

Now create your environment and ignore files:

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

โœ… This creates a Node project and ensures your secrets wonโ€™t end up in 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 connect to your backend
  • dotenv โ†’ Loads your API keys from .env securely
  • openai โ†’ Official OpenAI JavaScript client
  • nodemon โ†’ Auto-restarts the server when files change

Update your package.json to enable modern syntax:

{
"name": "openai-backend",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
}

๐Ÿ’ก The "type": "module" line lets you use import instead of require().


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.

โš ๏ธ Never commit .env to Git โ€” your .gitignore already protects it.


Make a file called 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 does:

  • Starts an Express server on port 8000
  • Enables CORS so your frontend can talk to it
  • Automatically parses JSON requests
  • Adds a simple test route to confirm itโ€™s working

Run:

Terminal window
npm run dev

โœ… You should see:

๐Ÿš€ Server running on http://localhost:8000
๐Ÿ“š Ready for OpenAI integration!

Test in your browser: http://localhost:8000
You should see:

{
"message": "๐Ÿค– OpenAI Backend is running!",
"status": "ready"
}

Or test in your terminal:

Terminal window
curl http://localhost:8000

โŒ โ€œCannot use import statementโ€
โœ… Add "type": "module" to package.json.

โŒ โ€œPort 8000 already in useโ€
โœ… Change PORT=8001 in .env.

โŒ โ€œcommand not found: npmโ€
โœ… Install Node.js from nodejs.org.

โŒ โ€œCannot find module โ€˜expressโ€™โ€
โœ… Run npm install again in the correct folder.


openai-backend/
โ”œโ”€โ”€ index.js # Main server file
โ”œโ”€โ”€ package.json # Project config
โ”œโ”€โ”€ .env # API keys (private)
โ”œโ”€โ”€ .gitignore # Protects secrets
โ””โ”€โ”€ node_modules/ # Dependencies

๐ŸŽ‰ Your backend is now ready for AI integration.

Next: Your First AI Endpoint โ€” time to give your server some brainpower!