# ๐ง Configuration File Guide
Welcome to MockM's configuration world! mm.config.js
is like your personal debugger, making the API Mock service run exactly according to your needs.
# ๐ Quick Start
No configuration file? No problem! MockM will start with default configuration, letting you experience immediately:
# Execute in any directory, MockM automatically uses default settings
mockm
# ๐ก Basic Configuration - Starting from Zero
Creating your first configuration file is very simple:
๐ mm.config.js
module.exports = {
port: 9000, // ๐ Service port
host: '0.0.0.0', // ๐ก Listen address
dataDir: './httpData' // ๐ Data storage directory
}
# ๐ฏ Advanced Configuration - Functional Configuration
When you need more flexible configuration, functional configuration comes in handy:
module.exports = (util) => {
// ๐ ๏ธ Utilize MockM's powerful utility library
return {
port: process.env.NODE_ENV === 'production' ? 80 : 9000,
// ๐ Dynamic configuration based on environment
db: {
users: util.libObj.mockjs.mock({
'list|10': [{
'id|+1': 1,
'name': '@name'
}]
}).list
},
// ๐ง Advanced feature configuration
api: {
'/api/status': (req, res) => {
res.json({
status: 'running',
timestamp: Date.now(),
environment: process.env.NODE_ENV || 'development'
})
}
}
}
}
# ๐ Practical Configuration Scenarios
# ๐ฑ Mobile Development Scenario
module.exports = {
port: 3001,
cors: true, // ๐ CORS support
static: './mobile-assets', // ๐ฑ Static resource directory
// ๐ Proxy real API for debugging
proxy: {
'/api/prod': 'https://api.production.com'
}
}
# ๐งช Test Environment Configuration
module.exports = (util) => {
return {
port: 9999,
// ๐ฒ Simulate various network conditions
api: {
'/api/slow': (req, res) => {
setTimeout(() => {
res.json({ message: 'Simulate slow network response' })
}, 3000)
},
'/api/error': (req, res) => {
// ๐จ Simulate error response for testing
res.status(500).json({ error: 'Internal server error' })
}
}
}
}
# ๐ข Team Collaboration Configuration
module.exports = (util) => {
const teamConfig = {
port: 8080,
// ๐ฅ Team shared interface definitions
api: {
// ๐ User management interfaces
'GET /api/users': 'data1',
'POST /api/users': 'data2',
// ๐๏ธ Product management interfaces
'GET /api/products': util.libObj.mockjs.mock({
'list|20-50': [{
'id|+1': 1,
'name': '@title(3,8)',
'price|100-9999': 1,
'category': '@pick(["Electronics", "Clothing", "Food", "Books"])'
}]
}).list
},
// ๐ Development environment security configuration
disable: process.env.NODE_ENV === 'production' ? ['record'] : []
}
return teamConfig
}
# ๐จ The Art of Configuration Files
# ๐ Configuration File Naming Rules
mm.config.js
- Standard configuration filemm.config.dev.js
- Development environment specificmm.config.prod.js
- Production environment specific
# ๐ Environment Variable Integration
module.exports = {
port: process.env.MOCK_PORT || 9000,
host: process.env.MOCK_HOST || 'localhost',
// ๐ฑ Enable different features based on environment
disable: process.env.NODE_ENV === 'production'
? ['record', 'history']
: []
}
# ๐จ Common Configuration Pitfalls
Port Conflicts ๐ฅ
// โ Avoid using system reserved ports port: 80 // Requires administrator privileges // โ Use development-friendly ports port: 9000
Path Configuration Errors ๐
// โ Relative paths may cause issues dataDir: '../data' // โ Use absolute paths or __dirname dataDir: path.join(__dirname, 'httpData')
# ๐ฏ Next Steps to Explore
Configuration files are just the beginning! Continue exploring more powerful features:
- ๐ง Advanced Configuration Options - Unlock all configuration possibilities
- ๐ ๏ธ Utility Library Usage - Master the powerful util toolkit
- ๐ API Configuration Art - Create professional-level API Mock
๐ก Pro Tip: Configuration files support hot reload, automatically taking effect after modifications for a smoother development experience!