Server Command
The fakelab serve command starts the Fakelab mock server. You can use it with or without a configuration file.
Usage
npx fakelab serve [options]
Options
| Option | Alias | Description |
|---|---|---|
--source | -s | Path to the source TypeScript file(s) or directory(s) |
--pathPrefix <prefix> | -x | Prefix for all generated API routes |
--locale <locale> | -l | Locale used for fake data generation |
--port <number> | -p | Port to run the server on |
Examples
Basic Usage
Start the server using the configuration file:
npx fakelab serve
Custom Source and Port
npx fakelab serve -s ./types -p 4000
Custom API Prefix and Locale
npx fakelab serve --pathPrefix /v1 --locale fr
Multiple Sources
npx fakelab serve -s ./types -s ./fixtures
Complete Example
npx fakelab serve \
--source ./types \
--source ./fixtures \
--pathPrefix /api/v1 \
--locale en \
--port 8080
Command Line vs Configuration File
Options provided via command line override configuration file settings:
# Configuration file specifies port 50000
# Command line overrides to 4000
npx fakelab serve -p 4000
Server Output
When the server starts, you'll see output like:
✓ Fakelab server started
Server running on: http://localhost:50000
API prefix: /api
Locale: en
Available endpoints:
- GET /api/User
- GET /api/Post
- POST /api/User
- POST /api/Post
API Endpoints
Fakelab automatically generates REST endpoints for each type:
GET /api/{TypeName}- Fetch mock data (supports?count=Nquery parameter)POST /api/{TypeName}- Insert data into database (if enabled)
Example Requests
# Fetch a single User
curl http://localhost:50000/api/User
# Fetch 10 Users
curl http://localhost:50000/api/User?count=10
# Insert a User (database mode)
curl -X POST http://localhost:50000/api/User
Environment Variables
You can also configure the server using environment variables:
FAKELAB_SOURCE=./types \
FAKELAB_PORT=4000 \
FAKELAB_PATH_PREFIX=/api/v1 \
FAKELAB_LOCALE=fr \
npx fakelab serve
Integration with npm scripts
Add Fakelab to your package.json:
{
"scripts": {
"mock": "fakelab serve",
"mock:dev": "fakelab serve -p 4000 -s ./fixtures"
}
}
Then run:
npm run mock
# or
npm run mock:dev
Troubleshooting
Port Already in Use
If the port is already in use, Fakelab will show an error:
Error: Port 50000 is already in use
Solution: Use a different port with -p:
npx fakelab serve -p 50001
No Types Found
If no types are found, check:
- Source path is correct
- TypeScript files contain exported interfaces/types
- Files are included in the source path
Server Not Responding
Check:
- Server is running (check terminal output)
- Correct port number
- Correct API prefix
- Network/firewall settings