{"id":13402178,"url":"https://github.com/senchalabs/connect","last_synced_at":"2025-05-12T03:45:03.968Z","repository":{"id":922800,"uuid":"687836","full_name":"senchalabs/connect","owner":"senchalabs","description":"Connect is a middleware layer for Node.js","archived":false,"fork":false,"pushed_at":"2024-09-27T16:10:21.000Z","size":5321,"stargazers_count":9870,"open_issues_count":8,"forks_count":1092,"subscribers_count":304,"default_branch":"master","last_synced_at":"2025-05-10T06:01:47.994Z","etag":null,"topics":["javascript","nodejs"],"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/senchalabs.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2010-05-26T18:55:41.000Z","updated_at":"2025-05-09T13:42:36.000Z","dependencies_parsed_at":"2024-10-06T05:06:06.396Z","dependency_job_id":"f2ffab36-658f-4d61-806e-f76e078c1808","html_url":"https://github.com/senchalabs/connect","commit_stats":{"total_commits":3287,"total_committers":158,"mean_commits":"20.803797468354432","dds":"0.42044417401886214","last_synced_commit":"98ab0c435852b311ed57fe48a1846c2dae84e266"},"previous_names":[],"tags_count":230,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/senchalabs%2Fconnect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/senchalabs%2Fconnect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/senchalabs%2Fconnect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/senchalabs%2Fconnect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/senchalabs","download_url":"https://codeload.github.com/senchalabs/connect/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253458579,"owners_count":21911887,"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":["javascript","nodejs"],"created_at":"2024-07-30T19:01:12.371Z","updated_at":"2025-05-12T03:45:03.937Z","avatar_url":"https://github.com/senchalabs.png","language":"JavaScript","readme":"\u003cdiv align=\"center\"\u003e\n\n\u003cimg src=\"logo/horizontal.png\" alt=\"connect logo\" width=\"450px\"\u003e\n\n[![NPM Version][npm-version-image]][npm-url]\n[![NPM Downloads][npm-downloads-image]][npm-url]\n[![Build Status][github-actions-ci-image]][github-actions-ci-url]\n[![Test Coverage][coveralls-image]][coveralls-url]\n\n\u003c/div\u003e\n\n  Connect is an extensible HTTP server framework for [node](http://nodejs.org) using \"plugins\" known as _middleware_.\n\n```js\nvar connect = require('connect');\nvar http = require('http');\n\nvar app = connect();\n\n// gzip/deflate outgoing responses\nvar compression = require('compression');\napp.use(compression());\n\n// store session state in browser cookie\nvar cookieSession = require('cookie-session');\napp.use(cookieSession({\n    keys: ['secret1', 'secret2']\n}));\n\n// parse urlencoded request bodies into req.body\nvar bodyParser = require('body-parser');\napp.use(bodyParser.urlencoded({extended: false}));\n\n// respond to all requests\napp.use(function(req, res){\n  res.end('Hello from Connect!\\n');\n});\n\n//create node.js http server and listen on port\nhttp.createServer(app).listen(3000);\n```\n\n## Getting Started\n\nConnect is a simple framework to glue together various \"middleware\" to handle requests.\n\n### Install Connect\n\n```sh\n$ npm install connect\n```\n\n### Create an app\n\nThe main component is a Connect \"app\". This will store all the middleware\nadded and is, itself, a function.\n\n```js\nvar app = connect();\n```\n\n### Use middleware\n\nThe core of Connect is \"using\" middleware. Middleware are added as a \"stack\"\nwhere incoming requests will execute each middleware one-by-one until a middleware\ndoes not call `next()` within it.\n\n```js\napp.use(function middleware1(req, res, next) {\n  // middleware 1\n  next();\n});\napp.use(function middleware2(req, res, next) {\n  // middleware 2\n  next();\n});\n```\n\n### Mount middleware\n\nThe `.use()` method also takes an optional path string that is matched against\nthe beginning of the incoming request URL. This allows for basic routing.\n\n```js\napp.use('/foo', function fooMiddleware(req, res, next) {\n  // req.url starts with \"/foo\"\n  next();\n});\napp.use('/bar', function barMiddleware(req, res, next) {\n  // req.url starts with \"/bar\"\n  next();\n});\n```\n\n### Error middleware\n\nThere are special cases of \"error-handling\" middleware. There are middleware\nwhere the function takes exactly 4 arguments. When a middleware passes an error\nto `next`, the app will proceed to look for the error middleware that was declared\nafter that middleware and invoke it, skipping any error middleware above that\nmiddleware and any non-error middleware below.\n\n```js\n// regular middleware\napp.use(function (req, res, next) {\n  // i had an error\n  next(new Error('boom!'));\n});\n\n// error middleware for errors that occurred in middleware\n// declared before this\napp.use(function onerror(err, req, res, next) {\n  // an error occurred!\n});\n```\n\n### Create a server from the app\n\nThe last step is to actually use the Connect app in a server. The `.listen()` method\nis a convenience to start a HTTP server (and is identical to the `http.Server`'s `listen`\nmethod in the version of Node.js you are running).\n\n```js\nvar server = app.listen(port);\n```\n\nThe app itself is really just a function with three arguments, so it can also be handed\nto `.createServer()` in Node.js.\n\n```js\nvar server = http.createServer(app);\n```\n\n## Middleware\n\nThese middleware and libraries are officially supported by the Connect/Express team:\n\n  - [body-parser](https://www.npmjs.com/package/body-parser) - previous `bodyParser`, `json`, and `urlencoded`. You may also be interested in:\n    - [body](https://www.npmjs.com/package/body)\n    - [co-body](https://www.npmjs.com/package/co-body)\n    - [raw-body](https://www.npmjs.com/package/raw-body)\n  - [compression](https://www.npmjs.com/package/compression) - previously `compress`\n  - [connect-timeout](https://www.npmjs.com/package/connect-timeout) - previously `timeout`\n  - [cookie-parser](https://www.npmjs.com/package/cookie-parser) - previously `cookieParser`\n  - [cookie-session](https://www.npmjs.com/package/cookie-session) - previously `cookieSession`\n  - [csurf](https://www.npmjs.com/package/csurf) - previously `csrf`\n  - [errorhandler](https://www.npmjs.com/package/errorhandler) - previously `error-handler`\n  - [express-session](https://www.npmjs.com/package/express-session) - previously `session`\n  - [method-override](https://www.npmjs.com/package/method-override) - previously `method-override`\n  - [morgan](https://www.npmjs.com/package/morgan) - previously `logger`\n  - [response-time](https://www.npmjs.com/package/response-time) - previously `response-time`\n  - [serve-favicon](https://www.npmjs.com/package/serve-favicon) - previously `favicon`\n  - [serve-index](https://www.npmjs.com/package/serve-index) - previously `directory`\n  - [serve-static](https://www.npmjs.com/package/serve-static) - previously `static`\n  - [vhost](https://www.npmjs.com/package/vhost) - previously `vhost`\n\nMost of these are exact ports of their Connect 2.x equivalents. The primary exception is `cookie-session`.\n\nSome middleware previously included with Connect are no longer supported by the Connect/Express team, are replaced by an alternative module, or should be superseded by a better module. Use one of these alternatives instead:\n\n  - `cookieParser`\n    - [cookies](https://www.npmjs.com/package/cookies) and [keygrip](https://www.npmjs.com/package/keygrip)\n  - `limit`\n    - [raw-body](https://www.npmjs.com/package/raw-body)\n  - `multipart`\n    - [connect-multiparty](https://www.npmjs.com/package/connect-multiparty)\n    - [connect-busboy](https://www.npmjs.com/package/connect-busboy)\n  - `query`\n    - [qs](https://www.npmjs.com/package/qs)\n  - `staticCache`\n    - [st](https://www.npmjs.com/package/st)\n    - [connect-static](https://www.npmjs.com/package/connect-static)\n\nCheckout [http-framework](https://github.com/Raynos/http-framework/wiki/Modules) for many other compatible middleware!\n\n## API\n\nThe Connect API is very minimalist, enough to create an app and add a chain\nof middleware.\n\nWhen the `connect` module is required, a function is returned that will construct\na new app when called.\n\n```js\n// require module\nvar connect = require('connect')\n\n// create app\nvar app = connect()\n```\n\n### app(req, res[, next])\n\nThe `app` itself is a function. This is just an alias to `app.handle`.\n\n### app.handle(req, res[, out])\n\nCalling the function will run the middleware stack against the given Node.js\nhttp request (`req`) and response (`res`) objects. An optional function `out`\ncan be provided that will be called if the request (or error) was not handled\nby the middleware stack.\n\n### app.listen([...])\n\nStart the app listening for requests. This method will internally create a Node.js\nHTTP server and call `.listen()` on it.\n\nThis is an alias to the `server.listen()` method in the version of Node.js running,\nso consult the Node.js documentation for all the different variations. The most\ncommon signature is [`app.listen(port)`](https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_server_listen_port_hostname_backlog_callback).\n\n### app.use(fn)\n\nUse a function on the app, where the function represents a middleware. The function\nwill be invoked for every request in the order that `app.use` is called. The function\nis called with three arguments:\n\n```js\napp.use(function (req, res, next) {\n  // req is the Node.js http request object\n  // res is the Node.js http response object\n  // next is a function to call to invoke the next middleware\n})\n```\n\nIn addition to a plan function, the `fn` argument can also be a Node.js HTTP server\ninstance or another Connect app instance.\n\n### app.use(route, fn)\n\nUse a function on the app, where the function represents a middleware. The function\nwill be invoked for every request in which the URL (`req.url` property) starts with\nthe given `route` string in the order that `app.use` is called. The function is\ncalled with three arguments:\n\n```js\napp.use('/foo', function (req, res, next) {\n  // req is the Node.js http request object\n  // res is the Node.js http response object\n  // next is a function to call to invoke the next middleware\n})\n```\n\nIn addition to a plan function, the `fn` argument can also be a Node.js HTTP server\ninstance or another Connect app instance.\n\nThe `route` is always terminated at a path separator (`/`) or a dot (`.`) character.\nThis means the given routes `/foo/` and `/foo` are the same and both will match requests\nwith the URLs `/foo`, `/foo/`, `/foo/bar`, and `/foo.bar`, but not match a request with\nthe URL `/foobar`.\n\nThe `route` is matched in a case-insensitive manner.\n\nIn order to make middleware easier to write to be agnostic of the `route`, when the\n`fn` is invoked, the `req.url` will be altered to remove the `route` part (and the\noriginal will be available as `req.originalUrl`). For example, if `fn` is used at the\nroute `/foo`, the request for `/foo/bar` will invoke `fn` with `req.url === '/bar'`\nand `req.originalUrl === '/foo/bar'`.\n\n## Running Tests\n\n```bash\nnpm install\nnpm test\n```\n\n## People\n\nThe Connect project would not be the same without all the people involved.\n\nThe original author of Connect is [TJ Holowaychuk](https://github.com/tj)\n\nThe current lead maintainer is [Douglas Christopher Wilson](https://github.com/dougwilson)\n\n[List of all contributors](https://github.com/senchalabs/connect/graphs/contributors)\n\n## License\n\n[MIT](LICENSE)\n\n[coveralls-image]: https://badgen.net/coveralls/c/github/senchalabs/connect/master\n[coveralls-url]: https://coveralls.io/r/senchalabs/connect?branch=master\n[github-actions-ci-image]: https://badgen.net/github/checks/senchalabs/connect/master?label=ci\n[github-actions-ci-url]: https://github.com/jshttp/senchalabs/connect?query=workflow%3Aci\n[npm-downloads-image]: https://badgen.net/npm/dm/connect\n[npm-url]: https://npmjs.org/package/connect\n[npm-version-image]: https://badgen.net/npm/v/connect\n","funding_links":[],"categories":["JavaScript","Web 后端","Socket server","1. 后端开发"],"sub_categories":["Run server","1.2 框架"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsenchalabs%2Fconnect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsenchalabs%2Fconnect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsenchalabs%2Fconnect/lists"}