| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
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.
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.
// 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:
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
| Back | FazBrowse Home | New Git URL |