{"id":17398490,"url":"https://github.com/vweevers/http-graceful","last_synced_at":"2025-03-29T20:15:35.579Z","repository":{"id":23821156,"uuid":"27197950","full_name":"vweevers/http-graceful","owner":"vweevers","description":"Gracefully open and close a http server, its sockets and perhaps a database","archived":false,"fork":false,"pushed_at":"2014-12-01T15:07:10.000Z","size":124,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T21:39:06.171Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vweevers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-26T21:57:49.000Z","updated_at":"2014-12-01T15:17:50.000Z","dependencies_parsed_at":"2022-09-02T05:41:08.421Z","dependency_job_id":null,"html_url":"https://github.com/vweevers/http-graceful","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vweevers%2Fhttp-graceful","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vweevers%2Fhttp-graceful/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vweevers%2Fhttp-graceful/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vweevers%2Fhttp-graceful/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vweevers","download_url":"https://codeload.github.com/vweevers/http-graceful/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246237436,"owners_count":20745348,"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-16T14:57:28.607Z","updated_at":"2025-03-29T20:15:35.543Z","avatar_url":"https://github.com/vweevers.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# http-graceful\n\n\u003eGracefully open and close a http server, its sockets and perhaps a database.\n\n## about\n\nCreates a function `start` that creates and opens a http server. Because `start` is an [AsyncEventEmitter](https://www.npmjs.org/package/async-eventemitter), you can add hooks, e.g. to open a database, that execute before or after the http server is opened. Likewise, you can add hooks that execute before or after the server is closed - either with `start.close()` or if the process receives a termination signal. Optionally, `http-graceful` will also close open connections.\n\nNote that if you don't call `start`, it will start in the next tick. Say you have an `app.js`, which exports `start`, like so:\n\n```js\nvar app = express()\n\nvar start = module.exports = httpGraceful(app, {\n  close_sockets: true,\n  sockets_timeout: 100\n})\n\nstart.on('before listen', function(opts, next){\n  openMyDatabase(function(err, db){\n    app.db = db\n    next(err)\n  })\n})\n\n// There's also a \"before close\" event, but\n// let's be safe and close the database \n// after all sockets have been closed.\nstart.on('close', function(opts, next){\n  app.db.close(next)\n})\n```\n\nTo start the server, the usual still works: `node app.js`. But also, in some other file:\n\n```js\nvar start = require('./app')\n\n// Bonus: you can override options\nstart({sockets_timeout: 200}, function(err, app){\n   // Server has started, database is open\n   doSomething()\n\n   // Or add your own options\n   start.close({foo: 'bar'}, function(){ \n      //..\n   })\n})\n\n// Without a callback argument, events are\n// synchronous (see `async-eventemitter`)\nstart.once('before close', function(opts){\n  assert(opts.foo==='bar')\n})\n```\n\nIf you call `start` when the server has already started, your callback will be called immediately and no events will be emitted. Same goes for `start.close()`.\n\nSee also the examples:\n\n    DEBUG=http-graceful node example/app.js \n    DEBUG=http-graceful node example/app-consumer.js \n\n## install\n\nWith [npm](https://npmjs.org) do:\n\n```\nnpm install http-graceful\n```\n\n## license\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvweevers%2Fhttp-graceful","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvweevers%2Fhttp-graceful","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvweevers%2Fhttp-graceful/lists"}