Fast HTTP Server for React Native
npm install @op-engineering/op-serverThe Request object is read-only and contains information about the incoming HTTP request.
method: string- The HTTP method (GET, POST, PUT, DELETE, etc.)path: string- The request path (e.g., "/api/users")body: string- The request body contentheaders: Record<string, string>- HTTP headers from the requestparams: Record<string, string>- Query string parameters
server.get('/api/user', async (req, res) => {
console.log(req.method); // "GET"
console.log(req.path); // "/api/user"
console.log(req.params); // { id: "123" } if URL was /api/user?id=123
console.log(req.headers); // { "user-agent": "...", ... }
res.statusCode = 200;
res.content = 'User data';
});The Response object is writable and allows you to set the HTTP response from JavaScript.
statusCode: number- HTTP status code (default: 200)content: string- Response body contentcontentType: string- Content-Type header (default: "text/plain")headers: Record<string, string>- Custom response headers (read-only, usesetHeaderto add)
setHeader(key: string, value: string): void- Add a custom header to the response
// Simple text response
server.get('/ping', async (_req, res) => {
res.statusCode = 200;
res.contentType = 'text/plain';
res.content = 'pong';
});
// JSON response
server.get('/api/data', async (req, res) => {
res.statusCode = 200;
res.contentType = 'application/json';
res.content = JSON.stringify({
success: true,
data: { id: 1, name: 'Example' },
});
});
// Custom headers
server.get('/custom', async (_req, res) => {
res.statusCode = 200;
res.contentType = 'application/json';
res.setHeader('X-Custom-Header', 'CustomValue');
res.setHeader('Cache-Control', 'no-cache');
res.content = JSON.stringify({ message: 'OK' });
});
// Error response
server.get('/error', async (_req, res) => {
res.statusCode = 404;
res.contentType = 'application/json';
res.content = JSON.stringify({ error: 'Not found' });
});If your handler throws an error, the server will automatically:
- Set
statusCodeto 500 - Set
contentto the error message - Use the error's message property if available
server.get('/error', async () => {
throw new Error('Something went wrong'); // Will return 500 with error message
});import { HttpServer } from '@op-engineering/op-server';
const server = new HttpServer();
// GET with query params
server.get('/user', async (req, res) => {
const userId = req.params.id;
res.statusCode = 200;
res.contentType = 'application/json';
res.content = JSON.stringify({
id: userId,
name: 'John Doe',
});
});
// POST with body
server.POST('/user', async (req, res) => {
const userData = JSON.parse(req.body);
res.statusCode = 201;
res.contentType = 'application/json';
res.setHeader('Location', '/user/123');
res.content = JSON.stringify({
success: true,
created: userData,
});
});
// Start server on port 3000
server.listen(3000);Support is possible but not implemented. Pay me to do it :)
MIT