Your local Express server is ready! Now we must deploy it to the cloud so that users around the world can access your API.
In this final phase lesson, we will secure our configurations using environment variables, handle cross-origin constraints, and deploy our server.
Advertisement
Nextsem Academy
Lesson: p5-4-deployment
Slide 1 / 6
P5.4: Backend Deployment
Your local Express server is ready! Now we must deploy it to the cloud so that users around the world can access your API.
In this final phase lesson, we will secure our configurations using environment variables, handle cross-origin constraints, and deploy our server.
Slide 2 / 6
1. Environment Variables (.env)
You must never commit secret credentials (like database passwords, API keys) directly into your git repository. Instead, store them in a local .env file and read them using dotenv.
By default, web browsers block frontend code (e.g. running on http://localhost:3000) from requesting resources from a backend API running on a different domain (e.g. http://localhost:5000). This is a security constraint called CORS.
To allow your frontend to query your backend API, install and configure the cors middleware:
>_Command Prompt
โโโ
npm install cors
Interactive Console
Example
const express = require('express');
const cors = require('cors');
const app = express();
// Enable CORS for all incoming requests
app.use(cors());
// Or limit to your frontend domain:
app.use(cors({
origin: 'https://nextsem.online'
}));
Interactive Code
Slide 4 / 6
3. Deployment Platforms
You can host Express backend APIs on several popular cloud hosting platforms: