Koa always returning 404 when using Axios for request
See original GitHub issueI have the following code…
export class OtherApplication{
constructor(){
this.app = new Koa();
this.app.use((ctx, next)=> {
console.log(ctx.request.path);
if(ctx.request.path === "/callback") next();
else ctx.redirect(`https:\/\/${AUTH_DOMAIN}/authorize?response_type=code&client_id=${AUTH_CLIENT_ID}&redirect_uri=${AUTH_REDIRECT}`);
})
this.hosted = OtherApplication.getHostedApp();
this.app.use(mount(this.hosted));
this.options = {
key: fs.readFileSync(KEY_FILE),
cert: fs.readFileSync(CERT_FILE)
}
this.server = http2.createSecureServer(this.options, this.app.callback());
this.server.on("error", (err) => console.error(err));
}
// TODO: Make own class
static getHostedApp(){
const app = new Koa();
const router = new Router();
router.get("/", function(ctx){
ctx.render('index');
})
router.get("/test",function(ctx){
ctx.body = "Test"
});
router.get("/callback",(ctx, next)=>{
const data = {
"grant_type":"authorization_code",
"client_id":AUTH_CLIENT_ID,
"code": ctx.request.query.code,
"redirect_uri": AUTH_REDIRECT
}
const options = {
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: qs.stringify(data),
url: `https:\/\/${AUTH_DOMAIN}/oauth/token`,
};
axios(options).then((response)=>{
console.log("Made it here " + JSON.stringify(jwtDecode(response.data.id_token)));
ctx.body = "Test";
})
});
app.use(router.routes());
return app;
}
start(){
const listenAsync = promisify(this.server.listen.bind( this.server ));
return listenAsync(PORT);
}
}
When I run and successfully sign in it throws a 404 with no serverside errors. The console log is correct so I am not sure why it is throwing a 404.
If I replace (This one does not work!!!)
axios(options).then((response)=>{
console.log("Made it here " + JSON.stringify(jwtDecode(response.data.id_token)));
ctx.body = "Test";
})
With (This one works!!!)
Promise.resolve("").then((response)=>{
ctx.body = "Test";
})
So why is Axios causing Koa to throw a 404? I also tried doing it as an “async” function but that didn’t help either, same exact response.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Koa Endpoint always return 404 - node.js - Stack Overflow
I've been running in a problem where I believe a call is returning too early and makes my response always a 404 -...
Read more >Axios post request returns 404 on all apis - Laracasts
Use the developer tools in your browser and check what uri it's giving the 404 on. It's most likely not pointing to the...
Read more >axios error 404 - You.com | The AI Search Engine You Control
Describe the bug Axios returns 404 for a valid URL. The error/promise rejection contains a data field with what seems to be the...
Read more >KOA route return 404 not found - Shopify Community
The server consoles the data, the client returns 404. Everything I'm reading says I need a middleware to complete the route, is that...
Read more >First steps with Koa.js - LogRocket Blog
npm init npm i koa koa-router koa-ejs axios. Fill in the information that the command line is going to ask you about your...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Using finally seemed to work for me…
Still not sure why
Unfortunately, I need more information. Remove all unnecessary code and provide a minimal reproducible example and I can take a look at it.
However, you probably have a middleware where you call next without returning it.
I think you need to read up on how promises work because this isn’t a Koa issue.