So I have a Typescript interface IResponse
:
interface IResponse {
responseToString(): string;
}
export default IResponse;
I then have 2 classes implementing it, RestResponse
and HTMLResponse
:
import IResponse from "./IResponse";
class RestResponse implements IResponse {
private response: string;
responseToString(): string {
return this.response;
}
constructor(response: string) {
this.response = response;
}
}
export default RestResponse;
import IResponse from "./IResponse";
class HTMLResponse implements IResponse {
private response: string;
constructor(response: string) {
this.response = response;
}
responseToString(): string {
return this.response;
}
}
export default HTMLResponse;
And then I am using the classes here:
import http from "http";
import url from "url";
import Route from "./Route";
import HTTPRequest from "./HTTPRequest";
import IResponse from "./IResponse";
class HextecCreator {
static createApp = (routes: Array<Route>) => {
return {
getRoutes: () => {
return routes;
},
run: (port: number) => {
http
.createServer(function (req, res) {
console.log(routes);
let data: any = [];
let correctRoute: Route[];
req
.on("data", (chunk) => {
data.push(chunk);
})
.on("end", () => {
console.log(req.url);
data = Buffer.concat(data).toString();
correctRoute = routes.filter(
(route) =>
route.getUrl() ==
url.parse(req.url as string, true).pathname
);
if (typeof correctRoute[0] != "object") {
res.write("Route not found");
res.end();
} else {
console.log(correctRoute);
let responseObj: IResponse = correctRoute[0].getHandlerFunc()(
new HTTPRequest(
req.method,
req.url,
url.parse(req.url as string, true).query,
data
)
);
res.write(responseObj.responseToString());
res.end();
}
});
})
.listen(port);
},
};
};
}
export default HextecCreator;
However, when I try to use my library, in this code:
const HextecCreator = require("hextec").HextecCreator;
const Route = require("hextec").Route;
const RestResponse = require("hextec").Route;
const rootHandler = () => {
return new RestResponse("Welcome to Hextec");
};
const app = HextecCreator.createApp([new Route("/", rootHandler)]);
app.run(8080);
the error shows as responseToString
isn't a function. Here is the exact error:
> demo@1.0.0 start /home/rohand/demo
> node App.js
[ Route { url: '/', handlerFunc: [Function: rootHandler] } ]
/
[ Route { url: '/', handlerFunc: [Function: rootHandler] } ]
/home/rohand/demo/node_modules/hextec/dist/HextecCreator.js:38
res.write(responseObj.responseToString());
^
TypeError: responseObj.responseToString is not a function
at IncomingMessage.req.on.on (/home/rohand/demo/node_modules/hextec/dist/HextecCreator.js:38:47)
at IncomingMessage.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! demo@1.0.0 start: `node App.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the demo@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/rohand/.npm/_logs/2020-07-24T00_16_59_072Z-debug.log
Why is that this happens? I know that the handler function should return a responseToString
function, as the RestResponse class implements IResponse
.
Never mind, I had fixed it. My RestResponse was imported as a Route.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.