问题
Nodejs api not working after socket hangup and gets resolved automatically.I have also added res.end() while sending the response Also added connection:close header in request so as to tell server to close connection after sending the response .This error occurs UNCEREMONIOUS and gets resolved automatically but it blocks some of the trailing apis.
#!/usr/bin/env node
var app = require('../app');
var debug = require('debug')('rkdemo:server');
var http = require('http');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server =http.createServer(app,function (req, res) {
if (req.url !== 'favicon.ico') {
console.log(':' + req.url, req.method)
req.on('data', function (data) {
console.log("Server Started");
console.log('data' + data);
res.end('post');
});
if (req.method.toUpperCase() == 'GET')
res.end('get');
} else {
res.end('NOT FOUND');
}
}).listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
return val;
}
if (port >= 0) {
return port;
}
return false;
}
function onError(error) {
console.error('error occurred.')
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
case 'ECONNRESET':
console.error('Time out error occurred.');
break;
case 'ETIMEDOUT':
console.error('Time out error occurred.');
break;
default:
console.error(error.stack +'error occurred');
break;
}
}
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
来源:https://stackoverflow.com/questions/52929170/nodejs-api-not-working-after-socket-hangup-and-gets-resolved-automatically