Skip to main content

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

VariableDefaultDescription
PORT3000The port number the server listens on
NODE_ENVdevelopmentEnvironment 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):
app.ts
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:
app.ts
const corsOptions = {
  origin: 'https://yourdomain.com',
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  allowedHeaders: ['Content-Type', 'Authorization']
};

CORS Options Reference

OptionTypeDescription
originstring | string[] | booleanAllowed origins or * for all
methodsstring[]Allowed HTTP methods
allowedHeadersstring[]Headers that can be used in requests
credentialsbooleanAllow cookies and authentication headers
maxAgenumberHow 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

src/routes/userRoutes.ts
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

1

Change Upload Directory

Modify the destination callback to use a different path:
destination: (req, file, cb) => {
  cb(null, path.join(__dirname, '../../my-uploads'));
}
2

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);
}
3

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:
tsconfig.json
{
  "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:
  1. Deploy to production - Build and deploy your API
  2. Test file uploads - Use the /users/upload endpoint

Build docs developers (and LLMs) love