{"id":14957494,"url":"https://github.com/oslabs-beta/connext-js","last_synced_at":"2025-04-13T05:27:19.101Z","repository":{"id":57205856,"uuid":"239546372","full_name":"oslabs-beta/connext-js","owner":"oslabs-beta","description":"A middleware and route handling solution for Next.js.","archived":false,"fork":false,"pushed_at":"2020-03-25T19:33:16.000Z","size":78,"stargazers_count":208,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-08T10:53:14.138Z","etag":null,"topics":["express","middleware","modularize","nextjs"],"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/oslabs-beta.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":"2020-02-10T15:35:32.000Z","updated_at":"2025-02-12T00:11:37.000Z","dependencies_parsed_at":"2022-09-18T01:01:37.967Z","dependency_job_id":null,"html_url":"https://github.com/oslabs-beta/connext-js","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/oslabs-beta%2Fconnext-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oslabs-beta%2Fconnext-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oslabs-beta%2Fconnext-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oslabs-beta%2Fconnext-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oslabs-beta","download_url":"https://codeload.github.com/oslabs-beta/connext-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248261995,"owners_count":21074229,"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":["express","middleware","modularize","nextjs"],"created_at":"2024-09-24T13:14:58.966Z","updated_at":"2025-04-13T05:27:19.065Z","avatar_url":"https://github.com/oslabs-beta.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Connext logo](https://i.ibb.co/kJpDpQG/connextlogo.png)\n\n[![Build Status](https://travis-ci.org/oslabs-beta/connext-js.svg?branch=master)](https://travis-ci.org/oslabs-beta/connext-js)\n\nA lightweight middleware and route handling solution for Next.js. Powered by [dirext](https://github.com/dirext-js/dirext) 🛸 \n\n# Install connext\n`$ npm install connext-js`\n\nInitialize a new instance of Connext. \n```javascript\nconst Connext = require('connext-js');\nconst app = Connext();\n```\n\n# Usage\n\nConnext is a middleware solution for Next.js with Express-style syntax that supports both global and flexible route-specific middleware. For global middleware, you must create a `controllers` folder that must contain a `global.js` controller file. We recommend also creating controller files for your other middleware as a way to modularize your API logic. \n\n## Setting Routes\nSetting routes using `connext`closely resembles setting routes in Express. \n\n### Method\nAll valid HTTP request methods have associated methods on the connext object. \n```javascript\napp.get();\napp.post();\napp.put();\napp.delete();\napp.head();\napp.trace();\napp.patch();\napp.options();\napp.connect();\n```\n\n### URL\nConnext supports any static or queried API endpoint. Support for dynamic routing is coming soon. ⚡️\n\n### Request \u0026 Response objects\nIn `connext`, you have access to the **request object** available in Next.js and that access persists through the middleware chain- just like Express! The available properties are `req.url`, `req.method`, and `req.body`.\n\nUnlike Express, if you need to store data you can add any key to the **response object** with whatever data you wish to store. This will simply augment your response object and continue to persist this object throughout the lifecycle of the request. \nEX:\n```javascript\nresponse.data = JSON.stringify(data);\nresponse.match = true;\nresponse.array = ['I'm the data you need'];\n```\n\n## Middleware\nExample file structure:\n```\n├── ...\n├── controllers\n│   ├── global.js         # required for global middleware\n│   └── middleware.js     # suggested for modularization of middleware functions                                 \n├── pages                 # required folder for routes in Next.js \n│   └── api               # required folder for API routes in Next.js \n└── ...                   \n```\n\n### Global Middleware\nTo utilize Connext's global middleware functionality, you must create a `global.js` file in a folder called `controllers`. The `controllers` folder must be at the same level as your `pages` folder and your `global.js` file must export an **array**. \n\n`connext` has a simple global built-in error handler that will run whenever something is passed into the invocation of `next()`. If you'd like to use your own error handler, define it in `global.js` as the **last** element of the exported array. \n\n**global.js example**\n```javascript\n// a global middleware function\nconst globalOne = (req, res, next) =\u003e {\n  console.log('this the first function that runs!');\n  return next();\n};\n// another global middleware function\nconst globalTwo = (req, res, next) =\u003e {\n  console.log('another one!');\n  return next();\n};\n// global error handler\nconst errorHandler = (err, req, res, next) =\u003e {\n  return res.status(500).send('an error occurred');\n};\n// export your array of global middleware\nmodule.exports = [globalOne, globalTwo, errorHandler];\n```\n\nWe recommend that you modularize your other middleware in one or more files in your `controllers` file to keep your code readable and easy to debug 🐞\n\n**middleware.js example**\n```javascript\nconst middlewareController = {};\n\nmiddlewareController.functionOne = (req, res, next) =\u003e {\n  // middleware functionality here\n  return next();\n}\n\nmiddlewareController.functionTwo = (req, res, next) =\u003e {\n  // middleware functionality here\n  return next();\n}\n\nmodule.exports = middlewareController;\n```\n\n### connext.METHOD(url, [...middleware])\n\nLike in express, every method in Connext has a string bound to it that coresponds with a valid HTTP method. \n\nFor example: `GET, DELETE, POST`, etc.\n\n## Use Case Example\n\nTo define a route using Connext, add a JavaScript file inside of Next.js's required `api` folder. \n\n```\n├── pages                       # required folder for routes in Next.js \n│   └── api                     # required folder for API routes in Next.js \n│       └── exampleRoute.js     # created route file inside of API folder     \n```\n\n**Inside of the route file**\n\n  1. Require in Connext and any route specific middleware controller files\n  2. Create a new instantiation of Connext\n  3. Set up routes by calling one of Connext-js's built in HTTP methods\n     * Pass the current route in as the first argument\n     * Chain any desired middleware functions in the order you want them to be invoked\n     * An anonymous middleware function can be defined at the end of the middleware chain. This function will end the request cycle.\n  4. You can invoke multiple HTTP methods in the same route file\n  5. Set your Connext-js invocation as the route files export default function\n\n``` javascript\nconst Connext = require('Connext-js');\nconst middleware = require('../../controllers/middleware');\n\nconst app = Connext();\n\napp.get('/api/exampleRoute', middleware.one, middleware.two, (req, res) =\u003e {\n  res.status(200).json(res.example);\n});\napp.post('/api/exampleRoute', middleware.three, (req, res) =\u003e {\n  res.status(200).json(res.example);\n});\napp.delete('/api/exampleRoute', middleware.four, (req, res) =\u003e {\n  res.status(200).json(res.example);\n});\n\nexport default app;\n```\n\n### Creators\n\n[Sara Powers](https://github.com/sarapowers)\n\n[Eli Gallipoli](https://github.com/egcg317)\n\n[Izumi Sato](https://github.com/izumi411)\n\n[Alex Kang](https://github.com/akang0408)\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foslabs-beta%2Fconnext-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foslabs-beta%2Fconnext-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foslabs-beta%2Fconnext-js/lists"}