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

Closed
alewarOS opened this Issue Jan 24, 2016 · 7 comments

Comments

6 participants
@alewarOS

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

This comment has been minimized.

Show comment
Hide comment
@menems

menems Jan 25, 2016

Contributor

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.

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

This comment has been minimized.

Show comment
Hide comment
@alewarOS

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

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

This comment has been minimized.

Show comment
Hide comment
@dead-horse

dead-horse Jan 27, 2016

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

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

This comment has been minimized.

Show comment
Hide comment
Member

tejasmanohar commented Jan 29, 2016

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

@jonathanong

This comment has been minimized.

Show comment
Hide comment
@jonathanong

jonathanong Mar 15, 2016

Member

would es6 classes even solve this issue?

Member

jonathanong commented Mar 15, 2016

would es6 classes even solve this issue?

@PlasmaPower

This comment has been minimized.

Show comment
Hide comment
@PlasmaPower

PlasmaPower Mar 17, 2016

Contributor

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

Contributor

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

@jonathanong

This comment has been minimized.

Show comment
Hide comment
@jonathanong

jonathanong May 14, 2016

Member

added docs here: e61d545

make a PR if you have any suggestions!

Member

jonathanong commented May 14, 2016

added docs here: e61d545

make a PR if you have any suggestions!

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