Introduction
Getting Started
Install and set up mock-config-server
Install the core package using your preferred package manager:
npm install mock-config-serverCreate a mock-server.config.ts file with server configuration and export it as default. This is the default file path, but you can change it.
import { MockServerConfig, rest } from 'mock-config-server';
export default [
{
baseUrl: '/api'
},
{
configs: [
rest.get('user', {
response: {
emoji: '🧊',
name: 'Siberia can code'
}
})
]
}
] satisfies MockServerConfig;import { MockServerConfig, graphql } from 'mock-config-server';
export default [
{
baseUrl: '/graphql'
},
{
configs: [
graphql.query('GetUser', {
response: { emoji: '🧊', name: 'Siberia can code' }
})
]
}
] satisfies MockServerConfig;Start the mock server:
npx mock-config-serverIf the package is already installed, you can use the short command
mcs
Now you can make a request:
fetch('http://localhost:31299/api/user')
.then((res) => res.json())
.then((data) => console.log(data));const body = JSON.stringify({
query: 'query GetUser { name }'
});
fetch('http://localhost:31299/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body
})
.then((response) => response.json())
.then((data) => console.log(data));Next Steps
This guide is a good starting point, but you can do so much more with mock-config-server. We highly encourage you to explore this documentation to learn more about the capabilities of Mock Config. Here are some great topics to follow up on: