import express from 'express' ; import next from 'next' ; import setupSitemapAndRobots from './setupSitemapAndRobots' ; import routesWithCache from './routesWithCache' ; const dev = process . env . NODE_ENV !== 'production' ; const port = process . env . PORT || 3000 ; const app = next ( { dev } ) ; const handle = app . getRequestHandler ( ) ; app . prepare ( ) . then ( ( ) => { const server = express ( ) ; server . get ( '/_next/*' , ( req , res ) => { handle ( req , res ) ; } ) ; server . use ( express . json ( ) ) ; if ( ! dev ) { server . set ( 'trust proxy' , 1 ) ; } server . get ( '/' , async ( req , res ) => { let redirectUrl = 'login' ; if ( req . user ) { if ( ! req . user . defaultTeamSlug ) { redirectUrl = 'create-team' ; } else { redirectUrl = `team/${ req . user . defaultTeamSlug }/discussions` ; } } res . redirect ( `${ dev ? process . env . NEXT_PUBLIC_URL_APP : process . env . NEXT_PUBLIC_PRODUCTION_URL_APP }/${ redirectUrl }` ) ; } ) ; server . get ( '/teams/:teamSlug/your-settings' , ( req , res ) => { const { teamSlug } = req . params ; app . render ( req , res , '/your-settings' , { teamSlug } ) ; } ) ; server . get ( '/teams/:teamSlug/team-settings' , ( req , res ) => { const { teamSlug } = req . params ; app . render ( req , res , '/team-settings' , { teamSlug } ) ; } ) ; server . get ( '/teams/:teamSlug/billing' , ( req , res ) => { const { teamSlug } = req . params ; app . render ( req , res , '/billing' , { teamSlug , ... ( req . query || { } ) } ) ; } ) ; server . get ( '/teams/:teamSlug/discussions/:discussionSlug' , ( req , res ) => { const { teamSlug , discussionSlug } = req . params ; app . render ( req , res , '/discussion' , { teamSlug , discussionSlug } ) ; } ) ; server . get ( '/teams/:teamSlug/discussions' , ( req , res ) => { const { teamSlug } = req . params ; app . render ( req , res , '/discussion' , { teamSlug } ) ; } ) ; server . get ( '/teams/:teamSlug/discussions-f/:discussionSlug' , ( req , res ) => { const { teamSlug , discussionSlug } = req . params ; app . render ( req , res , '/discussion-f' , { teamSlug , discussionSlug } ) ; } ) ; server . get ( '/teams/:teamSlug/discussions-f' , ( req , res ) => { const { teamSlug } = req . params ; app . render ( req , res , '/discussion-f' , { teamSlug } ) ; } ) ; server . get ( '/signup' , ( req , res ) => { app . render ( req , res , '/login' ) ; } ) ; setupSitemapAndRobots ( { server } ) ; routesWithCache ( { server , app } ) ; server . get ( '*' , ( req , res ) => { handle ( req , res ) ; } ) ; server . listen ( port , ( ) => { console . log ( `> Ready on ${ dev ? process . env . NEXT_PUBLIC_URL_APP : process . env . NEXT_PUBLIC_PRODUCTION_URL_APP }` ) ; } ) ; } ) ;