JavaScript mim Runtime API
The JavaScript mim Runtime API provides the objects and methods available to your mim code at runtime. Every mim request handler receives three objects:
mimikModule.exports = (context, request, response) => {
// context - runtime services (storage, http, env, info)
// request - incoming HTTP request data
// response - methods to send HTTP response
};
Handler Signature
Your mim exports a single function that mimOE calls for each incoming request:
import Router from 'router';
const app = Router({ mergeParams: true });
mimikModule.exports = (context, request, response) => {
global.context = context; // Make context available globally
app(request, response, (e) => {
response.end(JSON.stringify({ code: e ? 400 : 404, message: e || 'Not Found' }));
});
};
app.get('/hello', (request, response) => {
response.end('Hello World!');
});
app.get('/data', (request, response) => {
const value = context.storage.getItem('myKey');
response.end(JSON.stringify({ value }));
});
The Three Objects
context
The context object provides runtime services for your mim:
| Property | Type | Description |
|---|---|---|
context.http | object | HTTP client for outbound requests |
context.storage | object | Persistent key-value storage |
context.env | object | Environment variables passed at deployment |
context.info | object | Metadata about the mim and node |
context.dispatchWebSocketEvent | function | Dispatch WebSocket events (when enabled) |
request
The request object contains the incoming HTTP request:
| Property | Type | Description |
|---|---|---|
request.url | string | Full URL including query string |
request.method | string | HTTP method (GET, POST, etc.) |
request.headers | object | HTTP headers (lowercase keys) |
request.body | string | Request body (for POST/PUT) |
request.authorization | string | Authorization header value |
response
The response object is used to send the HTTP response:
| Property/Method | Description |
|---|---|
response.statusCode | Set HTTP status code (default: 200) |
response.setHeader(key, value) | Set a response header |
response.write(content) | Write to response stream |
response.end(body) | Send response and close connection |
Quick Examples
Simple GET endpoint
app.get('/status', (request, response) => {
response.end(JSON.stringify({ status: 'ok' }));
});
POST with JSON body
app.post('/users', (request, response) => {
const user = JSON.parse(request.body);
context.storage.setItem('user:' + user.id, JSON.stringify(user));
response.statusCode = 201;
response.end(JSON.stringify({ created: true }));
});
Making outbound HTTP requests
app.get('/external', (request, response) => {
context.http.request({
url: 'https://api.example.com/data',
success: (result) => {
response.end(result.data);
},
error: (err) => {
response.statusCode = 500;
response.end(err.message);
}
});
});
Using environment variables
app.get('/config', (request, response) => {
const apiKey = context.env.API_KEY;
const debug = context.env.DEBUG === 'true';
response.end(JSON.stringify({ debug }));
});
API Reference
| Page | Description |
|---|---|
| context.http | HTTP client for outbound requests |
| context.storage | Persistent key-value storage with tagging, embeddings, and pagination |
| context.info | Node metadata and environment variables |
| context.dispatchWebSocketEvent | WebSocket event dispatch for real-time notifications |
| request | Incoming HTTP request object |
| response | HTTP response methods |
Runtime Environment
The mimOE JavaScript runtime is NOT Node.js. It is a custom ES5-based runtime. Node.js built-in modules are not available.
ES5 with Transpilation
The runtime executes ES5 JavaScript, but you can write modern JavaScript and transpile it using Webpack with Babel:
// You can write modern JavaScript
const add = (a, b) => a + b;
const message = `Hello ${name}`;
const { data } = response;
// Webpack + Babel transpiles to ES5 for deployment
See the JavaScript mims guide for toolchain setup.
What's Available vs. What's Not
| Available | Not Available |
|---|---|
context.http | fetch, XMLHttpRequest |
context.storage | fs, localStorage |
JSON, Math, Date, RegExp | setTimeout, setInterval |
Array methods (map, filter, etc.) | Buffer (Node.js) |
console.log (debugging) | process, __dirname |
For the full list of constraints and workarounds, see Platform Constraints.
Related
- JavaScript mims: Development guide
- MCM API: Deploy and manage mims
- Architecture: How mims work