response
The response object is used to send HTTP responses back to the client. It is passed as the third parameter to your mim handler.
mimikModule.exports = (context, request, response) => {
// Use response to send data back
response.end('Hello World!');
};
Properties
| Property | Type | Default | Description |
|---|---|---|---|
statusCode | number | 200 | HTTP status code to send |
Methods
| Method | Description |
|---|---|
setHeader(key, value) | Set a response header |
write(content) | Write to response stream (for streaming) |
end(body) | Send response and close connection |
writeMimeFile(filePath, mimeType) | Send a file as response |
statusCode
Set the HTTP status code before calling end() or write().
app.get('/user/:id', (request, response) => {
var user = context.storage.getItem('user:' + request.params.id);
if (!user) {
response.statusCode = 404;
response.end('User not found');
return;
}
response.end(user);
});
Common Status Codes
// Success
response.statusCode = 200; // OK
response.statusCode = 201; // Created
response.statusCode = 204; // No Content
// Client errors
response.statusCode = 400; // Bad Request
response.statusCode = 401; // Unauthorized
response.statusCode = 403; // Forbidden
response.statusCode = 404; // Not Found
response.statusCode = 409; // Conflict
response.statusCode = 422; // Unprocessable Entity
// Server errors
response.statusCode = 500; // Internal Server Error
response.statusCode = 502; // Bad Gateway
response.statusCode = 503; // Service Unavailable
setHeader()
Sets an HTTP response header.
response.setHeader(key, value) → boolean
| Parameter | Type | Description |
|---|---|---|
key | string | Header name |
value | string | Header value |
Returns: true on success.
app.get('/data', (request, response) => {
response.setHeader('Content-Type', 'application/json');
response.setHeader('Cache-Control', 'no-cache');
response.setHeader('X-Custom-Header', 'custom-value');
response.end(JSON.stringify({ data: 'value' }));
});
Common Headers
// Content type
response.setHeader('Content-Type', 'application/json');
response.setHeader('Content-Type', 'text/html');
response.setHeader('Content-Type', 'text/plain');
// Caching
response.setHeader('Cache-Control', 'no-cache');
response.setHeader('Cache-Control', 'max-age=3600');
// CORS
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
response.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// Download
response.setHeader('Content-Disposition', 'attachment; filename="report.pdf"');
write()
Writes data to the response stream without closing the connection. Use for streaming responses.
response.write(content) → boolean
| Parameter | Type | Description |
|---|---|---|
content | string | Content to write |
Returns: true on success.
Throws: Error if client disconnected.
app.get('/stream', (request, response) => {
response.setHeader('Content-Type', 'text/plain');
response.write('Starting...\n');
response.write('Processing step 1...\n');
response.write('Processing step 2...\n');
response.write('Done!\n');
response.end();
});
Server-Sent Events (SSE)
app.get('/events', (request, response) => {
response.setHeader('Content-Type', 'text/event-stream');
response.setHeader('Cache-Control', 'no-cache');
response.setHeader('Connection', 'keep-alive');
// Send events
response.write('data: {"event": "connected"}\n\n');
response.write('data: {"event": "update", "value": 1}\n\n');
response.write('data: {"event": "update", "value": 2}\n\n');
response.end();
});
end()
Sends the response body and closes the connection.
response.end(body) → boolean
| Parameter | Type | Description |
|---|---|---|
body | string | Response body content (optional) |
Returns: true on success.