Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to extend the app's context in Koa@2 #652

Closed
alewaros opened this issue Jan 24, 2016 · 9 comments
Closed

Unable to extend the app's context in Koa@2 #652

alewaros opened this issue Jan 24, 2016 · 9 comments
Labels
Milestone

Comments

@alewaros
Copy link

@alewaros alewaros commented Jan 24, 2016

Trying to extend the context of the application since migrating to koa@2 throws this error:

TypeError: Cannot set property 'property_name' of #<Object> which has only a getter

A small example code

import Koa from 'koa';

const app = new Koa();
app.context.host = process.env.HOST || '0.0.0.0';
app.context.port = process.env.PORT || 8080;
@menems
Copy link
Contributor

@menems menems commented Jan 25, 2016

Hi,

The host property on the context is the host of the request object. not the host of your running server instance.

if you want to change your host:port you have to change it on the http listen function.

@alewaros
Copy link
Author

@alewaros alewaros commented Jan 25, 2016

It was just as an example that I picked the host port here. My bad. But any other parameters throws an error when trying to set something to context.

@dead-horse
Copy link
Member

@dead-horse dead-horse commented Jan 27, 2016

context.host a setter/getter, so if you want to override it, try:

Object.defineProperty(app.context, 'host', {
 get: function() {
   return '0.0.0.0'
 },
});
@dead-horse dead-horse added the question label Jan 27, 2016
@tejasmanohar
Copy link
Member

@tejasmanohar tejasmanohar commented Jan 29, 2016

@jonathanong jonathanong added this to the 2.0.0 milestone Jan 30, 2016
@jonathanong
Copy link
Member

@jonathanong jonathanong commented Mar 15, 2016

would es6 classes even solve this issue?

@PlasmaPower
Copy link
Contributor

@PlasmaPower PlasmaPower commented Mar 17, 2016

I don't think so, as delegate is still used for Request and Response.

@jonathanong jonathanong modified the milestones: v3.0.0, v2.0.0 Mar 23, 2016
@jonathanong jonathanong added the wontfix label May 14, 2016
jonathanong added a commit that referenced this issue May 14, 2016
references:

- #652
@jonathanong
Copy link
Member

@jonathanong jonathanong commented May 14, 2016

added docs here: e61d545

make a PR if you have any suggestions!

@duanchongchong
Copy link

@duanchongchong duanchongchong commented Oct 29, 2019

for(var i=0;i<100;i++){
console.log(i)
}

@xiaoxin999
Copy link

@xiaoxin999 xiaoxin999 commented Mar 19, 2020

6666666

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants
You can’t perform that action at this time.