[connect] Add missing property originalUrl to req parameter (#40776)

This adds a new class IncomingMessage which extends from
http.IncomingMessage and adds the originalUrl property which was
previously missing. This new class is used for the req parameter in
HandleFunction.

The originalUrl property is added to req here:
https://github.com/senchalabs/connect/blob/3.4.0/index.js#L133
This commit is contained in:
Trygve Aaberge
2019-12-17 13:09:53 +00:00
committed by Orta
parent 53cb7669d8
commit 7408c5dffc
2 changed files with 19 additions and 9 deletions
+12 -6
View File
@@ -4,23 +4,23 @@ import connect = require("connect");
const app = connect();
// log all requests
app.use((req: http.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => {
app.use((req: connect.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => {
console.log(req, res);
next();
});
// "Throw" an Error
app.use((req: http.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => {
app.use((req: connect.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => {
next(new Error("Something went wrong!"));
});
// "Throw" a number
app.use((req: http.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => {
app.use((req: connect.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => {
next(404);
});
// Stop on errors
app.use((err: any, req: http.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => {
app.use((err: any, req: connect.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => {
if (err) {
return res.end(`Error: ${err}`);
}
@@ -29,15 +29,21 @@ app.use((err: any, req: http.IncomingMessage, res: http.ServerResponse, next: co
});
// Use legacy `Function` for `next` parameter.
app.use((req: http.IncomingMessage, res: http.ServerResponse, next: Function) => {
app.use((req: connect.IncomingMessage, res: http.ServerResponse, next: Function) => {
next();
});
// respond to all requests
app.use((req: http.IncomingMessage, res: http.ServerResponse) => {
app.use((req: connect.IncomingMessage, res: http.ServerResponse) => {
res.end("Hello from Connect!\n");
});
// Allow http.IncomingMessage as the type for req
app.use((req: http.IncomingMessage, res: http.ServerResponse) => {
console.log(req, res);
res.end();
});
//create node.js http server and listen on port
http.createServer(app).listen(3000);
+7 -3
View File
@@ -18,11 +18,15 @@ declare function createServer(): createServer.Server;
declare namespace createServer {
export type ServerHandle = HandleFunction | http.Server;
export class IncomingMessage extends http.IncomingMessage {
originalUrl?: http.IncomingMessage["url"];
}
type NextFunction = (err?: any) => void;
export type SimpleHandleFunction = (req: http.IncomingMessage, res: http.ServerResponse) => void;
export type NextHandleFunction = (req: http.IncomingMessage, res: http.ServerResponse, next: NextFunction) => void;
export type ErrorHandleFunction = (err: any, req: http.IncomingMessage, res: http.ServerResponse, next: NextFunction) => void;
export type SimpleHandleFunction = (req: IncomingMessage, res: http.ServerResponse) => void;
export type NextHandleFunction = (req: IncomingMessage, res: http.ServerResponse, next: NextFunction) => void;
export type ErrorHandleFunction = (err: any, req: IncomingMessage, res: http.ServerResponse, next: NextFunction) => void;
export type HandleFunction = SimpleHandleFunction | NextHandleFunction | ErrorHandleFunction;
export interface ServerStackItem {