{"id":13799223,"url":"https://github.com/ktmud/koa-spa","last_synced_at":"2025-04-12T23:36:41.339Z","repository":{"id":12901410,"uuid":"15578492","full_name":"ktmud/koa-spa","owner":"ktmud","description":"pushState friendly static file server, with koa","archived":false,"fork":false,"pushed_at":"2014-02-22T15:10:12.000Z","size":208,"stargazers_count":17,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T23:35:41.427Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ktmud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-02T07:43:48.000Z","updated_at":"2021-08-10T10:56:33.000Z","dependencies_parsed_at":"2022-09-01T12:20:34.722Z","dependency_job_id":null,"html_url":"https://github.com/ktmud/koa-spa","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktmud%2Fkoa-spa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktmud%2Fkoa-spa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktmud%2Fkoa-spa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ktmud%2Fkoa-spa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ktmud","download_url":"https://codeload.github.com/ktmud/koa-spa/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647257,"owners_count":21139081,"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-08-04T00:01:00.014Z","updated_at":"2025-04-12T23:36:41.298Z","avatar_url":"https://github.com/ktmud.png","language":"JavaScript","readme":"# koa-spa\n\nCreate a Single Page Application(SPA) server with [koa](http://koajs.com).\nJust like what [pushserve](https://github.com/paulmillr/pushserve) does.\n\nThe goal is simple, make all routes under your SPA send the same `index.html`,\nwhile keep assets files (css, js) under the same directory accessible.\n\nThis is only a koa middleware,\nso you can decorate the server with your own middlewares,\nor integrate this \"pushState friendly\" functionality into\na _normal server_ (with server side routing).\n\nA custom 404 page is possible, too.\n\n## Example\n\n```javascript\nvar path_ = require('path');\n\nvar koa = require('koa');\nvar spa = require('koa-spa');\n\nexports.startServer = function(port, path) {\n  var routes = {};\n\n  // collect available routes\n  require('./app/routes')(spa.routeCollector(routes));\n\n  var app = koa();\n  app.use(spa(path_.join(__dirname, path), {\n     index: 'index.html',\n     404: '404.html',\n     routeBase: '/',\n     routes: routes\n  }));\n\n  // assets file requests will not run through middlewares below,\n  // but `index.html` requests will\n  // app.use(detectLanguage());\n\n  app.listen(port);\n};\n```\n\nOr you can handle 404 error programmatically:\n```\n  // add this after `app.use(spa...`\n  app.use(function* () {\n    if (this.status == 404) {\n      res.body = 'Nothing Here.';\n    }\n  });\n```\n\nIf your are using something like [brunch](http://brunch.io) and Backbone.js,\nthe `app/routes.js` required above would be your routes configuration for Backbones.js:\n\n```javascript\nmodule.exports = function(match) {\n  match('', 'home#index');\n  match('login', 'account#login');\n  match('logout', 'account#logout')\n};\n```\n\n## in Production\n\nThis module should not be used as a production server directly, since the static files\nare not compressed at all. But you can add a nginx proxy layer onto it.\n\n```\nhttp {\n\n    gzip  on;\n    gzip_vary on;\n    gzip_proxied any;\n    gzip_types *;\n\n    server {\n      listen 80;\n      server_name www.example.com;\n      root /var/www/yoursite/public/;\n      index index.html;\n      try_files $uri @koa_spa;\n      location @koa_spa {\n        proxy_pass http://127.0.0.1:3333;\n      }\n    }\n}\n```\n\nActually, you can just use nginx and do `try_files $uri index.html`.\nIf you are OK with returing 200 for a URI that definitely doesn't exits.\n\n\nOr to combine this middleware with [koa-compress](https://github.com/koajs/compress),\nthen you have a pure nodejs web server.\n\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2013-2014 Jesse Yang (http://ktmud.com)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n","funding_links":[],"categories":["仓库"],"sub_categories":["中间件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fktmud%2Fkoa-spa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fktmud%2Fkoa-spa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fktmud%2Fkoa-spa/lists"}