All Projects → oslabs-beta → Connext Js

oslabs-beta / Connext Js

Licence: other
A middleware and route handling solution for Next.js.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Connext Js

Connect Cas2
NodeJS implement of CAS(Central Authentication Service) client.
Stars: ✭ 91 (-56.87%)
Mutual labels:  middleware, express
Graphbrainz
A fully-featured GraphQL interface for the MusicBrainz API.
Stars: ✭ 130 (-38.39%)
Mutual labels:  middleware, express
Webpack Hmr 3 Ways
Three ways to set up your webpack hot module replacement: webpack-dev-server CLI, webpack-dev-server API, and express with webpack-hot-middleware.
Stars: ✭ 108 (-48.82%)
Mutual labels:  middleware, express
Redirect Ssl
Connect/Express middleware to enforce https using is-https
Stars: ✭ 81 (-61.61%)
Mutual labels:  middleware, express
Saas
Build your own SaaS business with SaaS boilerplate. Productive stack: React, Material-UI, Next, MobX, WebSockets, Express, Node, Mongoose, MongoDB. Written with TypeScript.
Stars: ✭ 2,720 (+1189.1%)
Mutual labels:  express, nextjs
Service Tools
Prepare your Node.js application for production
Stars: ✭ 89 (-57.82%)
Mutual labels:  middleware, express
Resource Router Middleware
🚴 Express REST resources as middleware mountable anywhere
Stars: ✭ 124 (-41.23%)
Mutual labels:  middleware, express
Rainbow
An Express router middleware for RESTful API base on file path.
Stars: ✭ 53 (-74.88%)
Mutual labels:  middleware, express
Next Graphql Blog
🖊 A Blog including a server and a client. Server is built with Node, Express & a customized GraphQL-yoga server. Client is built with React, Next js & Apollo client.
Stars: ✭ 152 (-27.96%)
Mutual labels:  express, nextjs
Nextjs Express Mongoose Crudify Boilerplate
Next.js (React) + Redux + Express REST API + MongoDB + Mongoose-Crudify boilerplate
Stars: ✭ 148 (-29.86%)
Mutual labels:  express, nextjs
Bbs node
node后端服务,node+express+mysql, 搭建token-权限-管理完整的web服务, 对应页面: https://www.lyh.red/admin \ https://www.lyh.red/bbs \ 接口地址https://www.lyh.red/apidoc/index.html
Stars: ✭ 78 (-63.03%)
Mutual labels:  middleware, express
Host Validation
Express.js middleware for "Host" and "Referer" header validation to protect against DNS rebinding attacks.
Stars: ✭ 183 (-13.27%)
Mutual labels:  middleware, express
Express Joi Validation
validate express application inputs and parameters using joi
Stars: ✭ 70 (-66.82%)
Mutual labels:  middleware, express
Corser
CORS middleware for Node.js
Stars: ✭ 90 (-57.35%)
Mutual labels:  middleware, express
Http Proxy Middleware
⚡ The one-liner node.js http-proxy middleware for connect, express and browser-sync
Stars: ✭ 8,730 (+4037.44%)
Mutual labels:  middleware, express
Graphql Serverless
GraphQL (incl. a GraphiQL interface) middleware for the webfunc serverless web framework.
Stars: ✭ 124 (-41.23%)
Mutual labels:  middleware, express
Apicache
Simple API-caching middleware for Express/Node.
Stars: ✭ 957 (+353.55%)
Mutual labels:  middleware, express
Webpack Isomorphic Dev Middleware
The webpack-dev-middleware, but for isomorphic applications
Stars: ✭ 38 (-81.99%)
Mutual labels:  middleware, express
Nextjs Express
A simple app using Next.js
Stars: ✭ 136 (-35.55%)
Mutual labels:  express, nextjs
Next Session
Simple promise-based session middleware for Next.js, micro, Express, and more
Stars: ✭ 161 (-23.7%)
Mutual labels:  middleware, nextjs

Connext logo

Build Status

A lightweight middleware and route handling solution for Next.js. Powered by dirext 🛸

Install connext

$ npm install connext-js

Initialize a new instance of Connext.

const Connext = require('connext-js');
const app = Connext();

Usage

Connext 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.

Setting Routes

Setting routes using connextclosely resembles setting routes in Express.

Method

All valid HTTP request methods have associated methods on the connext object.

app.get();
app.post();
app.put();
app.delete();
app.head();
app.trace();
app.patch();
app.options();
app.connect();

URL

Connext supports any static or queried API endpoint. Support for dynamic routing is coming soon. ⚡️

Request & Response objects

In 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.

Unlike 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. EX:

response.data = JSON.stringify(data);
response.match = true;
response.array = ['I'm the data you need'];

Middleware

Example file structure:

├── ...
├── controllers
│   ├── global.js         # required for global middleware
│   └── middleware.js     # suggested for modularization of middleware functions                                 
├── pages                 # required folder for routes in Next.js 
│   └── api               # required folder for API routes in Next.js 
└── ...                   

Global Middleware

To 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.

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.

global.js example

// a global middleware function
const globalOne = (req, res, next) => {
  console.log('this the first function that runs!');
  return next();
};
// another global middleware function
const globalTwo = (req, res, next) => {
  console.log('another one!');
  return next();
};
// global error handler
const errorHandler = (err, req, res, next) => {
  return res.status(500).send('an error occurred');
};
// export your array of global middleware
module.exports = [globalOne, globalTwo, errorHandler];

We 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 🐞

middleware.js example

const middlewareController = {};

middlewareController.functionOne = (req, res, next) => {
  // middleware functionality here
  return next();
}

middlewareController.functionTwo = (req, res, next) => {
  // middleware functionality here
  return next();
}

module.exports = middlewareController;

connext.METHOD(url, [...middleware])

Like in express, every method in Connext has a string bound to it that coresponds with a valid HTTP method.

For example: GET, DELETE, POST, etc.

Use Case Example

To define a route using Connext, add a JavaScript file inside of Next.js's required api folder.

├── pages                       # required folder for routes in Next.js 
│   └── api                     # required folder for API routes in Next.js 
│       └── exampleRoute.js     # created route file inside of API folder     

Inside of the route file

  1. Require in Connext and any route specific middleware controller files
  2. Create a new instantiation of Connext
  3. Set up routes by calling one of Connext-js's built in HTTP methods
    • Pass the current route in as the first argument
    • Chain any desired middleware functions in the order you want them to be invoked
    • An anonymous middleware function can be defined at the end of the middleware chain. This function will end the request cycle.
  4. You can invoke multiple HTTP methods in the same route file
  5. Set your Connext-js invocation as the route files export default function
const Connext = require('Connext-js');
const middleware = require('../../controllers/middleware');

const app = Connext();

app.get('/api/exampleRoute', middleware.one, middleware.two, (req, res) => {
  res.status(200).json(res.example);
});
app.post('/api/exampleRoute', middleware.three, (req, res) => {
  res.status(200).json(res.example);
});
app.delete('/api/exampleRoute', middleware.four, (req, res) => {
  res.status(200).json(res.example);
});

export default app;

Creators

Sara Powers

Eli Gallipoli

Izumi Sato

Alex Kang

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].