Environment Variables
API Master uses environment variables to configure runtime behavior. Create a .env file in the root directory:
PORT=3000
NODE_ENV=development
Available Variables
| Variable | Default | Description |
|---|
PORT | 3000 | The port number the server listens on |
NODE_ENV | development | Environment mode (development or production) |
The PORT variable is read in app.ts:29:const PORT = process.env.PORT || 3000;
CORS Configuration
CORS (Cross-Origin Resource Sharing) is configured in app.ts to control which domains can access your API.
Default Configuration
The default CORS setup allows all origins (suitable for development):
const corsOptions = {
origin: '*', // Allow all origins (for development)
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization']
};
app.use(cors(corsOptions));
The default origin: '*' configuration is not recommended for production. Always specify exact domains in production environments.
Production Configuration
For production, modify the CORS options to specify allowed domains:
Single Domain
Multiple Domains
Environment-Based
const corsOptions = {
origin: 'https://yourdomain.com',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization']
};
const corsOptions = {
origin: ['https://yourdomain.com', 'https://app.yourdomain.com'],
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization']
};
const corsOptions = {
origin: process.env.NODE_ENV === 'production'
? 'https://yourdomain.com'
: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true
};
CORS Options Reference
| Option | Type | Description |
|---|
origin | string | string[] | boolean | Allowed origins or * for all |
methods | string[] | Allowed HTTP methods |
allowedHeaders | string[] | Headers that can be used in requests |
credentials | boolean | Allow cookies and authentication headers |
maxAge | number | How long preflight results are cached (seconds) |
Multer Storage Configuration
File uploads are handled by Multer with disk storage configured in src/routes/userRoutes.ts.
Current Configuration
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.join(__dirname, '../../uploads'));
},
filename: (req, file, cb) => {
const uniqueName = 'file-' + Date.now() + '-' +
Math.random().toString(36).substr(2, 9) +
path.extname(file.originalname);
cb(null, uniqueName);
}
});
const upload = multer({ storage: storage });
Customizing File Storage
Change Upload Directory
Modify the destination callback to use a different path:destination: (req, file, cb) => {
cb(null, path.join(__dirname, '../../my-uploads'));
}
Customize Filename Pattern
Adjust the filename callback for different naming schemes:filename: (req, file, cb) => {
// Use original filename with timestamp prefix
const uniqueName = Date.now() + '-' + file.originalname;
cb(null, uniqueName);
}
Add File Filtering
Restrict file types by adding a fileFilter:const upload = multer({
storage: storage,
fileFilter: (req, file, cb) => {
// Only allow images
if (file.mimetype.startsWith('image/')) {
cb(null, true);
} else {
cb(new Error('Only image files are allowed'));
}
},
limits: {
fileSize: 5 * 1024 * 1024 // 5MB limit
}
});
Uploaded files are served statically via the /uploads route configured in app.ts:18:app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
TypeScript Configuration
The tsconfig.json file controls TypeScript compilation behavior:
{
"compilerOptions": {
"target": "ES2020", // Target JavaScript version
"module": "commonjs", // Module system for Node.js
"lib": ["ES2020"], // Standard library features
"outDir": "./dist", // Compiled output directory
"rootDir": "./", // Source root directory
"strict": true, // Enable strict type checking
"esModuleInterop": true, // Enable ES module interop
"skipLibCheck": true, // Skip type checking of .d.ts files
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true // Allow importing .json files
},
"include": ["**/*.ts"],
"exclude": ["node_modules", "dist"]
}
Key Configuration Options
- target:
ES2020 - Uses modern JavaScript features
- module:
commonjs - Compatible with Node.js
- strict:
true - Enforces type safety
- outDir:
./dist - Output location for compiled files
Changing the outDir requires updating the build scripts in package.json and the main entry point.
Next Steps
After configuring your application:
- Deploy to production - Build and deploy your API
- Test file uploads - Use the
/users/upload endpoint