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

Comments

@alewaros
Copy link

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 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

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

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'
 },
});

@tejasmanohar
Copy link
Member

#605

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

would es6 classes even solve this issue?

@PlasmaPower
Copy link
Contributor

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 added a commit that referenced this issue May 14, 2016
@jonathanong
Copy link
Member

added docs here: e61d545

make a PR if you have any suggestions!

@duanchongchong
Copy link

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

@xiaoxin999
Copy link

6666666

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

No branches or pull requests

8 participants