I'm building a small API with Node 's HTTP module plus Typescript . The problem is that when I save the changes and make the first request with GET or another verb, the server crashes. If I send another request, it just works there. I've worked with HTTP before, but I started migrating to Typescript and this happened to me.
The test code is as follows:
const http = require('http');
http.createServer((req: any, res: any): any => {
const { url, method }: { url: string, method: string } = req;
if(url === '/' && method === 'GET') {
return res.end(JSON.stringify({ code: 200, notification: 'OK' });
};
res.end(JSON.stringify({ code: 200, notification: 'error' }));
}).listen(4000);
At first this happens:
And on the second try:
I tried to do it with promises but it keeps working wrong.
In the code that you have passed you need to close a parenthesis, inside the return of the
if
, I do not know if it is due to that, I have executed your code and I do not get the problem that you mention, I have used Postman andcurl
but there is no error in the first request .