ola, estou com esse erro no meu app:
Servidor Rodando! URL: / Method: GET _http_outgoing.js:482 throw new ERR_HTTP_HEADERS_SENT('set'); ^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:482:11) at Server.http.createServer (C:\Users\Samuel Melo da Silva\Desktop\node.js\index.js:13:8) at Server.emit (events.js:193:13) at parserOnIncoming (_http_server.js:680:12) at HTTPParser.parserOnHeadersComplete (_http_common.js:113:17)
codigo:
const http = require('http');
let server = http.createServer((req, res)=>{
console.log('URL:', req.url);
console.log('Method:', req.method);
res.end('ok');
switch(req.url){
case '/':
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end('<h1>Olá</h1>');
break;
case '/users':
res.statusCode = 200;
res.setHeader("Content-Type", 'application/json');
res.end(JSON.stringify({
user:[{
name:'Samuel',
email: '[email protected]',
id: 1
}]
}));
break;
}
});
server.listen(300, '127.0.0.1', ()=>{
console.log('Servidor Rodando!');
});