{"id":16473121,"url":"https://github.com/binki/autoserve","last_synced_at":"2025-04-04T17:15:43.409Z","repository":{"id":57187647,"uuid":"66337077","full_name":"binki/autoserve","owner":"binki","description":"Deploy a node webapp to FastCGI, Passenger, node http, or other things in the future","archived":false,"fork":false,"pushed_at":"2016-09-30T05:41:11.000Z","size":49,"stargazers_count":1,"open_issues_count":6,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-12T01:26:46.349Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/binki.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-23T05:48:59.000Z","updated_at":"2016-09-28T03:38:33.000Z","dependencies_parsed_at":"2022-08-28T13:00:30.787Z","dependency_job_id":null,"html_url":"https://github.com/binki/autoserve","commit_stats":null,"previous_names":["binki/http-autodetect"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binki%2Fautoserve","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binki%2Fautoserve/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binki%2Fautoserve/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binki%2Fautoserve/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/binki","download_url":"https://codeload.github.com/binki/autoserve/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247217220,"owners_count":20903009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T12:25:23.882Z","updated_at":"2025-04-04T17:15:43.383Z","avatar_url":"https://github.com/binki.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Automatically adapt any `http.createServer()`-compatible service to\nany deployment platform.\n\nOne might deploy a node-based webapp to any of the following\nplatforms:\n\n* [`http`](https://nodejs.org/api/http.html)\n* [`node-fastcgi`](https://github.com/fbbdev/node-fastcgi)\n* Phusion Passenger\n* Custom Platform (see docs below)\n\nBut how do you support all these platforms at once? By using an\nabstraction layer such as `autoserve`!\n\n# Usage\n\nTo write an application, simply pass the function you would pass to\n`http.createServer()` to `autoserve()`:\n\n`script.cgi`:\n\n    #!/usr/bin/env node\n    'use strict';\n    \n    const autoserve = require('autoserve');\n    \n    const app = function (req, res) {\n        res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8', });\n        // It is recommended to use a framework like express to calculate\n        // the host and protocol of your deployment. baseUrl only\n        // solves the path portion of the deployment.\n        res.end(`Hello, World! I am deployed at https://${req.headers.host}${req.baseUrl}`);\n    };\n    \n    autoserve(app);\n\nThat works OK for a very simple development server or with\n`mod_fcgid`, but doesn’t solve the issue of deploying to a highly\nspecialized platform. If the above script is published as an npm\nmodule named `example-autoserve-app`, you can configure `autoserve`\nprior to `require()`ing your app’s module. This way, you can keep the\ndeployment-specific configuration separate from your application\nlogic:\n\n`index.cgi`:\n\n    #!/usr/bin/env node\n    'use strict';\n    \n    const autoserve = require('autoserve');\n    \n    // Register a custom platform for the deployment\n    // environment.\n    autoserve.register({\n        detect: () =\u003e true,\n        name: 'annoying-http',\n        priority: 100,\n        serve: function (app, options) {\n            require('http').createServer(app).listen(options.port, function () {\n                require('child_process').exec(`:; xdg-open http://localhost:${options.port}/; exit $?\\nSTART http://localhost:${options.port}/`);\n            });\n        },\n    });\n    \n    // Override a registered platform’s property. Here\n    // we lower the core http platform’s priority so that\n    // our custom one will win (yes, this example is\n    // contrived).\n    autoserve.override('http', {\n        priority: 99,\n    });\n    \n    // Set options for this environment.\n    autoserve.extendOptions({\n        'annoying-http': {\n            port: 1025+1000*Math.random()|0,\n        },\n    });\n    \n    // The deployed app will see the configuration set\n    // above when it runs.\n    require('example-autoserve-app');\n\n## Express Usage\n\nExpress doesn’t seem to handle `request.baseUrl` being set. Use\n[express-autoserve](https://github.com/binki/express-autoserve).\n\n# API\n\n## Module\n\nThe module is invokable and behaves the same as `serve()`.\n\n    const autoserve = require('autoserve');\n\n### `serve(app)`\n\nYou normally call this by calling the module itself:\n\n    autoserve(function (request, response) {\n        …\n    });\n\nThis is analogous to the following snippet which directly uses the\n[`http`](https://nodejs.org/api/http.html) module. In fact, when the\n`http` platform is chosen by `detect()`, it will be equivalent.\n\n    const http = require('http');\n    http.createServer(function (request, response) {\n        …\n    }).listen(3000);\n\nThe parameters\n[`request`](https://nodejs.org/api/http.html#http_class_http_incomingmessage)\nand\n[`response`](https://nodejs.org/api/http.html#http_class_http_serverresponse)\nshall mimic the behavior of the equivalent `http` module-provided\nobjects. However, many platforms other than `http` will only support a\nsubset of operations. An application wishing to be portable can assume\nthat the following APIs are present:\n\n* `request`\n  * [`headers`](https://nodejs.org/api/http.html#http_message_headers)\n  * [`method`](https://nodejs.org/api/http.html#http_message_method)\n  * [`url`](https://nodejs.org/api/http.html#http_message_url)\n* `response`\n  * [`end`](https://nodejs.org/api/http.html#http_response_end_data_encoding_callback)\n  * [`write`](https://nodejs.org/api/http.html#http_response_write_chunk_encoding_callback) (though the platform might buffer everything instead of streaming chunks).\n  * [`writeHead`](https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers)\n\nMany platforms strive to provide undocumented and unmentioned APIs on\n`request` and `response` to support popular frameworks such as\nExpress.\n\n`autoserve` also defines the following properties:\n\n* `request`\n\n  * `baseUrl`: The URI used to access the script. This is useful for\n    applications which want to generate fully qualified internal links\n    which is useful when issuing redirects (the [HTTP `Location:`\n    header](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30)\n    requires fully qualified URIs).\n\n    Note that the URI provided here excludes host and protocol\n    fields—it will begin with a `/`. In fact, it will behave quite\n    like [Express’s `req.baseUrl`\n    property](https://expressjs.com/en/4x/api.html#req.baseUrl).\n\n    The `http` module does not provide this property because it\n    doesn’t support mounting applications at anything other than\n    `/`. However, in many deployment scenarios, an application will be\n    made accessible as a subtree of a host. This might be accomplished\n    using rewrites/aliases or implicitly based on the application’s\n    position in the filesystem hierarchy.\n\n    For example, Apache’s `mod_fcgid` might be configured as follows.\n    This would result in\n    [`autoserve-platform-node-fastcgi`](https://github.com/binki/autoserve-platform-node-fastcgi)\n    being detected and used:\n\n        # FilesMatch is more secure (exhibiting less confusing behavior) than AddHandler.\n        \u003cFilesMatch \"\\.cgi$\"\u003e\n            SetHandler fcgid-script\n            Options +ExecCGI\n        \u003c/FilesMatch\u003e\n\n    And a request might come in for\n    `/some/path/script.cgi/sub/resource`. In this case, `baseUrl` will\n    be `/some/path/script.cgi` while `url` will be\n    `/some/path/script.cgi/sub/resource`. Thus, `baseUrl` is\n    implicitly calculated by where the script is placed in the\n    filesystem. If you had a `RewriteRule` or `Alias` that runs\n    `script.cgi` when `/some/path/` is accessed, the `baseUrl` would\n    be explicitly set to the aliased path.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinki%2Fautoserve","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinki%2Fautoserve","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinki%2Fautoserve/lists"}