Nextjs middleware error while rewrite the route

I am facing an issue with middleware
Failed to proxy http://app.localhost:3000/app/login Error: getaddrinfo ENOTFOUND app.localhost
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:118:26)
at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'app.localhost'
}
Failed to proxy http://app.localhost:3000/app/login Error: getaddrinfo ENOTFOUND app.localhost
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:118:26)
at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'app.localhost'
}
1 Reply
Chandra
ChandraOP6mo ago
import { type NextRequest, NextResponse } from 'next/server';

import { DEFAULT_LOGIN_REDIRECT, authRoutes } from '@/routes';
import authConfig from '@drafton/auth/config';
import NextAuth from 'next-auth';
import type { Session } from 'next-auth/types';

const { auth } = NextAuth(authConfig);

interface NextAuthRequest extends NextRequest {
auth: Session | null;
}
import { type NextRequest, NextResponse } from 'next/server';

import { DEFAULT_LOGIN_REDIRECT, authRoutes } from '@/routes';
import authConfig from '@drafton/auth/config';
import NextAuth from 'next-auth';
import type { Session } from 'next-auth/types';

const { auth } = NextAuth(authConfig);

interface NextAuthRequest extends NextRequest {
auth: Session | null;
}
export default auth((req: NextAuthRequest) => {
const url = req.nextUrl;
const isLoggedIn = !!req.auth;

const isAuthRoute = authRoutes.includes(url.pathname);

// Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000)
let hostname = req.headers
.get('host')!
.replace('.localhost:3000', `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`);

hostname = hostname.replace('www.', ''); // remove www. from domain

const searchParams = req.nextUrl.searchParams.toString();
// Get the pathname of the request (e.g. /, /about, /blog/first-post)
const path = `${url.pathname}${
searchParams.length > 0 ? `?${searchParams}` : ''
}`;

if (hostname === `app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`) {
if (!isLoggedIn && !isAuthRoute) {
return NextResponse.redirect(new URL('/login', req.url));
} else if (isLoggedIn && isAuthRoute) {
return NextResponse.redirect(new URL(DEFAULT_LOGIN_REDIRECT, req.url));
}

// Continue to the next middleware or serve the root content
return NextResponse.rewrite(
new URL(`/app${path === '/' ? '' : path}`, req.url),
);
}
// rewrite root application to `/home` folder
if (
hostname === 'localhost:3000' ||
hostname === process.env.NEXT_PUBLIC_ROOT_DOMAIN
) {
return NextResponse.rewrite(
new URL(`/home${path === '/' ? '' : path}`, req.url),
);
}

// rewrite everything else to `/[domain]/[path] dynamic route
return NextResponse.rewrite(new URL(`/${hostname}${path}`, req.url));
});

// Optionally, don't invoke Middleware on some paths
export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};
export default auth((req: NextAuthRequest) => {
const url = req.nextUrl;
const isLoggedIn = !!req.auth;

const isAuthRoute = authRoutes.includes(url.pathname);

// Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000)
let hostname = req.headers
.get('host')!
.replace('.localhost:3000', `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`);

hostname = hostname.replace('www.', ''); // remove www. from domain

const searchParams = req.nextUrl.searchParams.toString();
// Get the pathname of the request (e.g. /, /about, /blog/first-post)
const path = `${url.pathname}${
searchParams.length > 0 ? `?${searchParams}` : ''
}`;

if (hostname === `app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`) {
if (!isLoggedIn && !isAuthRoute) {
return NextResponse.redirect(new URL('/login', req.url));
} else if (isLoggedIn && isAuthRoute) {
return NextResponse.redirect(new URL(DEFAULT_LOGIN_REDIRECT, req.url));
}

// Continue to the next middleware or serve the root content
return NextResponse.rewrite(
new URL(`/app${path === '/' ? '' : path}`, req.url),
);
}
// rewrite root application to `/home` folder
if (
hostname === 'localhost:3000' ||
hostname === process.env.NEXT_PUBLIC_ROOT_DOMAIN
) {
return NextResponse.rewrite(
new URL(`/home${path === '/' ? '' : path}`, req.url),
);
}

// rewrite everything else to `/[domain]/[path] dynamic route
return NextResponse.rewrite(new URL(`/${hostname}${path}`, req.url));
});

// Optionally, don't invoke Middleware on some paths
export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};
Want results from more Discord servers?
Add your server