Middleware
Framework Koa.js Route Middleware
Categories:
Assign the guest middleware to the different routes
const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
function guestMiddleware() {
return async (ctx, next) => {
// Check if the user is authenticated
const authToken = ctx.cookies.get('authToken');
if (authToken) {
ctx.throw(401, 'You are already authenticated');
}
// If the user is not authenticated, continue with the request
await next();
};
}
Assign the guest middleware separately to the different routes
// Assign the guest middleware separately to the different routes
router
.get('/api/auth/sign-in', guestMiddleware, UserSignInProcessFunction)
.get('/api/auth/sign-up', guestMiddleware, UserSignUpProcessFunction)
Assign the guest middleware with an array of paths
// Assign the guest middleware with an array of paths
router.use([
'/api/auth/sign-in',
'/api/auth/sign-up'
], guestMiddleware);
router
.get('/api/auth/sign-in', UserSignInProcessFunction)
.get('/api/auth/sign-up', UserSignUpProcessFunction)
Assign the global middleware before
other custom middleware
Assign the global middlware to the all routes
const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
// middleware function for all routes
const globalMiddleware = async (ctx, next) => {
console.log('Global middleware');
await next();
}
// middleware function for users route
const usersMiddleware = async (ctx, next) => {
console.log('Users middleware');
await next();
}
// assign globalMiddleware before all routes
router.use(globalMiddleware);
// define routes
router.get('/users', usersMiddleware, async (ctx, next) => {
ctx.body = 'User list';
});
router.get('/posts', async (ctx, next) => {
ctx.body = 'Post list';
});
Assign the global middlware to the specific route
// middleware function for all routes
const globalMiddleware = async (ctx, next) => {
console.log('Global middleware');
await next();
}
// middleware function for users route
const usersMiddleware = async (ctx, next) => {
console.log('Users middleware');
await next();
}
// define routes
router.get('/users', globalMiddleware, usersMiddleware, async (ctx, next) => {
ctx.body = 'User list';
});
router.get('/posts', async (ctx, next) => {
ctx.body = 'Post list';
});
Assign the global middleware after
other custom middleware
Assign the global middlware to the all routes
// middleware function for all routes
const globalMiddleware = async (ctx, next) => {
console.log('Global middleware');
await next();
}
// middleware function for users route
const usersMiddleware = async (ctx, next) => {
console.log('Users middleware');
await next();
}
// define routes
router.get('/users', usersMiddleware, async (ctx, next) => {
ctx.body = 'User list';
});
router.get('/posts', async (ctx, next) => {
ctx.body = 'Post list';
});
// assign globalMiddleware after all routes
router.use(globalMiddleware);
Assign the global middlware to the specific route
// middleware function for all routes
const globalMiddleware = async (ctx, next) => {
console.log('Global middleware');
await next();
}
// middleware function for users route
const usersMiddleware = async (ctx, next) => {
console.log('Users middleware');
await next();
}
// define routes
router.get('/users', usersMiddleware, globalMiddleware, async (ctx, next) => {
ctx.body = 'User list';
});
router.get('/posts', async (ctx, next) => {
ctx.body = 'Post list';
});
koa.js middleware example
Guest middleware
function guestMiddleware() {
return async (ctx, next) => {
// Check if the user is authenticated
const authToken = ctx.cookies.get('authToken');
if (authToken) {
ctx.throw(401, 'You are already authenticated');
}
// If the user is not authenticated, continue with the request
await next();
};
}
Custom response message
function guestMiddleware() {
return async (ctx, next) => {
// Check if the user is authenticated
const authToken = ctx.cookies.get('authToken');
if (authToken) {
ctx.status = 401; // Unauthorized
ctx.body = { error: 'You are already authenticated' };
return;
}
// If the user is not authenticated, continue with the request
await next();
};
}
Auth middleware
function authMiddleware() {
return async (ctx, next) => {
// Check if auth token cookie exists
const authToken = ctx.cookies.get('authToken');
if (!authToken) {
// Unauthorized
ctx.throw(401, 'Authentication required');
}
// If auth token cookie exists, continue with the request
await next();
};
}
Custom response message
function authMiddleware() {
return async (ctx, next) => {
// Check if auth token cookie exists
const authToken = ctx.cookies.get('authToken');
if (!authToken) {
ctx.status = 401; // Unauthorized
ctx.body = { error: 'Authentication required' };
return;
}
// If auth token cookie exists, continue with the request
await next();
};
}