jwarkentin / node-monkey

A Node.js module for inspecting and debugging Node applications through a web browser
MIT License
143 stars 16 forks source link

RangeError: Maximum call stack size exceeded #46

Open OleileiA opened 7 years ago

OleileiA commented 7 years ago

my code is simple:

const nodeMonkey = require('node-monkey');
const Koa = require('koa');
nodeMonkey();
const app = new Koa();
app.use((ctx) => {
    let url = ctx.request.url;
    ctx.body = url;
    console.log(ctx);
});
app.listen(3000);

i want to log 'ctx' in chrome. but i got this problem:

RangeError: Maximum call stack size exceeded
      at ServerResponse.get (_http_outgoing.js:114:16)
      at ServerResponse.getHeaders (/Users/cyw/Desktop/learnByself/koa2-gitbook-node/node_modules/restify/lib/response.js:177:17)
      at ServerResponse.get (_http_outgoing.js:115:17)
      at ServerResponse.getHeaders (/Users/cyw/Desktop/learnByself/koa2-gitbook-node/node_modules/restify/lib/response.js:177:17)
      at ServerResponse.get (_http_outgoing.js:115:17)
      at ServerResponse.getHeaders (/Users/cyw/Desktop/learnByself/koa2-gitbook-node/node_modules/restify/lib/response.js:177:17)
      at ServerResponse.get (_http_outgoing.js:115:17)
      at ServerResponse.getHeaders (/Users/cyw/Desktop/learnByself/koa2-gitbook-node/node_modules/restify/lib/response.js:177:17)
      at ServerResponse.get (_http_outgoing.js:115:17)
      at ServerResponse.getHeaders (/Users/cyw/Desktop/learnByself/koa2-gitbook-node/node_modules/restify/lib/response.js:177:17)

there is no recursive call in my code, how this happen?

jwarkentin commented 7 years ago

That is definitely strange. I will have to try and reproduce it in a few hours (after I get a little sleep). Whatever it is, it shouldn't be too difficult to sort out.

jwarkentin commented 7 years ago

Hey, sorry I wasn't able to get back to you yesterday. The issue here is some sort of bad interaction with Restify. I will have to look into it this weekend. The good news is, Node Monkey has a feature that allows you to provide your own server as documented here. Since I don't have a Koa example there yet, here is some code based on yours that will work:

const NodeMonkey = require('node-monkey')
const Koa = require('koa')
const mount = require('koa-mount')
const send = require('koa-send')

const app = new Koa()
const monk = NodeMonkey({
  server: {
    server: app.listen(3000)
  }
})

const monkPaths = monk.getServerPaths()
app.use(mount('/monkey.js', async ctx => await send(ctx, monkPaths.client, { root: monkPaths.basePath })))
app.use(mount('/monkey', async ctx => await send(ctx, monkPaths.index, { root: monkPaths.basePath })))

app.use(async (ctx, next) => {
  await next()

  let url = ctx.request.url
  ctx.body = url
  console.log(ctx)
})

Note that you'll have to install koa-mount and koa-send for those to work. To access the Node Monkey output you should then open a new tab, open your dev console and go to http://localhost:3000/monkey to see output from your app. Also, keep in mind that even though your code sets ctx.body you will not actually see it in the object dumped to the console because if you look in the Koa source code you'll notice ctx.body only exists as a delegated getter/setter. Just didn't want you to be confused when you don't see it there.

OleileiA commented 7 years ago

thanks for your advice, i'll try to understand what you told me about ctx.body. Don't worry about the code, relax and have fun at the weekend:)