Proper way to extend express Response Object using Typescript
I'm currently working on a project that involves Typescript 3+, and express 4+ and node 8+. I'm trying to extend express's Response object to send HTTP status codes if an API detects an error for example. I can't seem to figure out how to extend the Response object without using Middleware to define the Response extended function I want to add. I would love to be able to just use the Response prototype to define the function(s) i want to add but i don't know or can't figure out if this is possible. Here's what I've done:
projectA/declarations/express.extensions.d.ts:
import * as e from "express"
declare global
namespace Express
interface Response
send100(): e.Response;
export
below is where i would like to define the prototype function definition like so but this isn't compiling...
projectA/extensions/ResponseExtensions.ts
import * as e from "express";
import Response from "express-serve-static-core";
Response.prototype.send100 = function(): e.Response
var response = this as Response;
response.status(100).end();
and then finally consume...
ProjectB/Server.ts
import * as express from "express"
app.get('/getData'
request: express.Request,
response: express.Response) : void
// yada yada...
response.send100();
I can't seem to get the ResponseExtensions.ts script to transpile, And the only other way i see to implement this is to run middleware where i define the function, but this will happen for every single request. Are there options like I'm exploring or is middleware the only way to go?
Thanks.
node.js typescript express
add a comment |
I'm currently working on a project that involves Typescript 3+, and express 4+ and node 8+. I'm trying to extend express's Response object to send HTTP status codes if an API detects an error for example. I can't seem to figure out how to extend the Response object without using Middleware to define the Response extended function I want to add. I would love to be able to just use the Response prototype to define the function(s) i want to add but i don't know or can't figure out if this is possible. Here's what I've done:
projectA/declarations/express.extensions.d.ts:
import * as e from "express"
declare global
namespace Express
interface Response
send100(): e.Response;
export
below is where i would like to define the prototype function definition like so but this isn't compiling...
projectA/extensions/ResponseExtensions.ts
import * as e from "express";
import Response from "express-serve-static-core";
Response.prototype.send100 = function(): e.Response
var response = this as Response;
response.status(100).end();
and then finally consume...
ProjectB/Server.ts
import * as express from "express"
app.get('/getData'
request: express.Request,
response: express.Response) : void
// yada yada...
response.send100();
I can't seem to get the ResponseExtensions.ts script to transpile, And the only other way i see to implement this is to run middleware where i define the function, but this will happen for every single request. Are there options like I'm exploring or is middleware the only way to go?
Thanks.
node.js typescript express
add a comment |
I'm currently working on a project that involves Typescript 3+, and express 4+ and node 8+. I'm trying to extend express's Response object to send HTTP status codes if an API detects an error for example. I can't seem to figure out how to extend the Response object without using Middleware to define the Response extended function I want to add. I would love to be able to just use the Response prototype to define the function(s) i want to add but i don't know or can't figure out if this is possible. Here's what I've done:
projectA/declarations/express.extensions.d.ts:
import * as e from "express"
declare global
namespace Express
interface Response
send100(): e.Response;
export
below is where i would like to define the prototype function definition like so but this isn't compiling...
projectA/extensions/ResponseExtensions.ts
import * as e from "express";
import Response from "express-serve-static-core";
Response.prototype.send100 = function(): e.Response
var response = this as Response;
response.status(100).end();
and then finally consume...
ProjectB/Server.ts
import * as express from "express"
app.get('/getData'
request: express.Request,
response: express.Response) : void
// yada yada...
response.send100();
I can't seem to get the ResponseExtensions.ts script to transpile, And the only other way i see to implement this is to run middleware where i define the function, but this will happen for every single request. Are there options like I'm exploring or is middleware the only way to go?
Thanks.
node.js typescript express
I'm currently working on a project that involves Typescript 3+, and express 4+ and node 8+. I'm trying to extend express's Response object to send HTTP status codes if an API detects an error for example. I can't seem to figure out how to extend the Response object without using Middleware to define the Response extended function I want to add. I would love to be able to just use the Response prototype to define the function(s) i want to add but i don't know or can't figure out if this is possible. Here's what I've done:
projectA/declarations/express.extensions.d.ts:
import * as e from "express"
declare global
namespace Express
interface Response
send100(): e.Response;
export
below is where i would like to define the prototype function definition like so but this isn't compiling...
projectA/extensions/ResponseExtensions.ts
import * as e from "express";
import Response from "express-serve-static-core";
Response.prototype.send100 = function(): e.Response
var response = this as Response;
response.status(100).end();
and then finally consume...
ProjectB/Server.ts
import * as express from "express"
app.get('/getData'
request: express.Request,
response: express.Response) : void
// yada yada...
response.send100();
I can't seem to get the ResponseExtensions.ts script to transpile, And the only other way i see to implement this is to run middleware where i define the function, but this will happen for every single request. Are there options like I'm exploring or is middleware the only way to go?
Thanks.
node.js typescript express
node.js typescript express
asked Nov 15 '18 at 5:18
lvlosslvloss
254
254
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The Response
member of express-serve-static-core
is only an interface, not a class with a prototype you can extend, so when you refer to Response
as a value, TypeScript resolves it to an unrelated Response
class that is part of the DOM API. The prototype object you want to extend is the response
member of the express
module (see this answer). It isn't declared in @types/express
, but you can declare it yourself:
declare module "express"
const response: Response;
Then in ResponseExtensions.ts
, you can write:
import * as e from "express";
import Response from "express-serve-static-core";
e.response.send100 = function(): e.Response
var response = this as Response;
response.status(100).end();
Thanks Matt! this worked out great for me.
– lvloss
Nov 16 '18 at 7:10
add a comment |
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312889%2fproper-way-to-extend-express-response-object-using-typescript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The Response
member of express-serve-static-core
is only an interface, not a class with a prototype you can extend, so when you refer to Response
as a value, TypeScript resolves it to an unrelated Response
class that is part of the DOM API. The prototype object you want to extend is the response
member of the express
module (see this answer). It isn't declared in @types/express
, but you can declare it yourself:
declare module "express"
const response: Response;
Then in ResponseExtensions.ts
, you can write:
import * as e from "express";
import Response from "express-serve-static-core";
e.response.send100 = function(): e.Response
var response = this as Response;
response.status(100).end();
Thanks Matt! this worked out great for me.
– lvloss
Nov 16 '18 at 7:10
add a comment |
The Response
member of express-serve-static-core
is only an interface, not a class with a prototype you can extend, so when you refer to Response
as a value, TypeScript resolves it to an unrelated Response
class that is part of the DOM API. The prototype object you want to extend is the response
member of the express
module (see this answer). It isn't declared in @types/express
, but you can declare it yourself:
declare module "express"
const response: Response;
Then in ResponseExtensions.ts
, you can write:
import * as e from "express";
import Response from "express-serve-static-core";
e.response.send100 = function(): e.Response
var response = this as Response;
response.status(100).end();
Thanks Matt! this worked out great for me.
– lvloss
Nov 16 '18 at 7:10
add a comment |
The Response
member of express-serve-static-core
is only an interface, not a class with a prototype you can extend, so when you refer to Response
as a value, TypeScript resolves it to an unrelated Response
class that is part of the DOM API. The prototype object you want to extend is the response
member of the express
module (see this answer). It isn't declared in @types/express
, but you can declare it yourself:
declare module "express"
const response: Response;
Then in ResponseExtensions.ts
, you can write:
import * as e from "express";
import Response from "express-serve-static-core";
e.response.send100 = function(): e.Response
var response = this as Response;
response.status(100).end();
The Response
member of express-serve-static-core
is only an interface, not a class with a prototype you can extend, so when you refer to Response
as a value, TypeScript resolves it to an unrelated Response
class that is part of the DOM API. The prototype object you want to extend is the response
member of the express
module (see this answer). It isn't declared in @types/express
, but you can declare it yourself:
declare module "express"
const response: Response;
Then in ResponseExtensions.ts
, you can write:
import * as e from "express";
import Response from "express-serve-static-core";
e.response.send100 = function(): e.Response
var response = this as Response;
response.status(100).end();
answered Nov 15 '18 at 14:34
Matt McCutchenMatt McCutchen
14.2k820
14.2k820
Thanks Matt! this worked out great for me.
– lvloss
Nov 16 '18 at 7:10
add a comment |
Thanks Matt! this worked out great for me.
– lvloss
Nov 16 '18 at 7:10
Thanks Matt! this worked out great for me.
– lvloss
Nov 16 '18 at 7:10
Thanks Matt! this worked out great for me.
– lvloss
Nov 16 '18 at 7:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312889%2fproper-way-to-extend-express-response-object-using-typescript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown