koajs / static

Static file server middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blocking Downstream Middleware?

TimothyMischief opened this issue · comments

If I set defer to true it will hit the last middleware block as intended and serves the homepage but won't serve static content. If I don't defer it returns 404 and never hits the last middleware block and won't serve the Apollo stuff at the index route (there's no other routes defined by react-router yet to test, index page is rendered dynamically by the server). I tried setting index opts to false in case it was trying to serve a static index that didn't exist.

I've tried running with debug on and I still can't figure out where it's going wrong but I'm not getting anywhere at all. Throwing random logs through all the files to try to trace it. Thought someone might be able to point out a silly mistake that's staring me right in the face.

Thanks heaps!

github repo for reference: @SnappyCroissant/ts-koapollo

Contents of './public':

-public
--bundle.js
--bundle.js.map

relevant part of package.json:

    "koa": "^2.0.0-alpha.7",
    "koa-bodyparser": "^3.2.0",
    "koa-cors": "0.0.16",
    "koa-proxy": "^0.6.0",
    "koa-route": "^3.2.0",
    "koa-router": "^7.0.1",
    "koa-static": "^3.0.0",

File in question:

// imports and definitions

export default class ReactServer {
	server: koa;

	constructor ( port: number, callback?: Function, apiPort?: number ) {

		const basePort = port;
		apiPort = apiPort || config.graphQLServer.PORT;
		const apiHost = `http://localhost:${ apiPort }`;
		const apiUrl = `${ apiHost }/graphql`;
		const scriptUrl = `http://localhost:${ basePort }/bundle.js`;

		this.server = new koa();
		this.server.use( koaStatic( path.join( process.cwd(), 'public' ) ) );

		this.server.use( async ( ctx: koa.Context, next: Function ) => {

			// ApolloClient Rendering/React Router Routing Stuff

		});

		this.server.listen( port, callback() );
	}
}

Okay so I've discovered it's not blocking downstream but it seems to be responding without waiting for downstream to process. So it's throwing a 404 back to the client then running the rest of the query. My problem is probably in my middleware. Closing the issue.