{"library":"pixl-server-api","title":"JSON REST API Component for pixl-server","description":"pixl-server-api is a specialized component designed for the `pixl-server` framework, enabling the creation of JSON REST APIs. Built on `pixl-server-web`, it facilitates defining API methods or entire classes that respond to configurable URL patterns such as `/api/METHOD_NAME` or `/api/NAMESPACE/METHOD_NAME`. The package is currently at version 1.0.9, indicating a stable release. While no explicit release cadence is mentioned, the `pixl-server` ecosystem tends towards stability with infrequent, targeted updates rather than rapid iteration. Its primary differentiator is its seamless integration and low-boilerplate approach within the `pixl-server` architecture, making it suitable for applications already leveraging that foundation. It focuses on straightforward JSON response handling.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install pixl-server-api"],"cli":null},"imports":["const APIComponent = require('pixl-server-api');","server.API.addHandler(...);","server.API.addHandler('my_method', function(args, callback) { ... });"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const PixlServer = require('pixl-server');\nconst path = require('path');\n\nconst server = new PixlServer({\n    __name: 'MyApiServer',\n    __version: \"1.0\",\n\n    config: {\n        log_dir: path.join(__dirname, 'logs'),\n        debug_level: 9,\n\n        WebServer: {\n            http_port: 8080,\n            http_htdocs_dir: path.join(__dirname, 'www')\n        },\n\n        API: {\n            base_uri: '/api'\n        }\n    },\n\n    components: [\n        require('pixl-server-web'),\n        require('pixl-server-api')\n    ]\n});\n\nserver.startup(function() {\n    console.log('Server started on port 8080. Try GET http://localhost:8080/api/add_user');\n\n    server.API.addHandler('add_user', function(args, callback) {\n        console.log('Received add_user API call with args:', args);\n        // Simulate an asynchronous operation\n        setTimeout(() => {\n            const username = args.username || 'guest';\n            if (username === 'error') {\n                callback({\n                    code: 1,\n                    description: \"Username 'error' is not allowed.\"\n                });\n            } else {\n                callback({\n                    code: 0,\n                    description: `User '${username}' added successfully!`,\n                    data: { id: Date.now(), username: username }\n                });\n            }\n        }, 100);\n    });\n\n    // Example: Shut down the server after a period for demonstration\n    setTimeout(() => {\n        console.log('Shutting down server.');\n        server.shutdown();\n    }, 10000);\n});","lang":"javascript","description":"This example demonstrates how to set up a `pixl-server` with `pixl-server-api` to expose a simple `/api/add_user` endpoint, handling request arguments and sending JSON responses, including a simulated error case.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}