Showing posts with label nodejs. Show all posts
Showing posts with label nodejs. Show all posts

Saturday, April 4, 2020

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client



Error Blog 

This blog is about the error Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Problem Statement

unable to start node js express application

Error Details 

error Log

ERROR DETAILS 

\node_modules\mysql\lib\protocol\Parser.js:437
      throw err; // Rethrow non-MySQL errors
      ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at ServerResponse.header (F:\ \study\gitRepo\new\learn\nodejs\expressjs\node_modules\express\lib\response.js:771:10)
    at ServerResponse.send (F:\ \study\gitRepo\new\learn\nodejs\expressjs\node_modules\express\lib\response.js:170:12)
    at ServerResponse.json (F:\ \study\gitRepo\new\learn\nodejs\expressjs\node_modules\express\lib\response.js:267:15)
    at F:\ \study\gitRepo\new\learn\nodejs\expressjs\service\users.js:24:11
    at Query.<anonymous> (F:\ \study\gitRepo\new\learn\nodejs\expressjs\service\task.service.js:20:21)
    at Query.<anonymous> (F:\ \study\gitRepo\new\learn\nodejs\expressjs\node_modules\mysql\lib\Connection.js:526:10)
    at Query._callback (F:\ \study\gitRepo\new\learn\nodejs\expressjs\node_modules\mysql\lib\Connection.js:488:16)
    at Query.Sequence.end (F:\ \study\gitRepo\new\learn\nodejs\expressjs\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
    at Query._handleFinalResultPacket (F:\ \study\gitRepo\new\learn\nodejs\expressjs\node_modules\mysql\lib\protocol\sequences\Query.js:149:8)




Error Code 

You will face this error if you res.send twice in an express js application.




var express = require('express');
var router = express.Router();
var Task = require('./task.service.js');

router.get('/'function(reqres){
   Task.getAllTask(function(errtask) {

      console.log('controller')
      if (err)
        res.send(err);
        console.log('res'task);
      res.send(task);
    });
});
router.post('/create'function(reqres){
   console.log(req.body);
   var new_task = new Task(req.body);
   console.log(new_task);
   Task.createTask(new_taskfunction(errtask) {
    
      if (err)
        res.send(err);
      res.json(task);
    });
   res.send('success.');

});

//export this router to use in our index.js
module.exports = router;



  

Solution

change the code so that res.send is not called twice . .

Code Changes 

add the following import in your component.ts file .

  
        
      if (err)
        res.send(err);
      res.json(task);
    });
  // res.send('success.');

         

OUTPUT
works 



Thanks for reading . Let me know your thoughts in the comments below    .

Saturday, March 28, 2020

ReferenceError: bodyParser is not defined



Error Blog 

Hi Guys ,
This blog is about the error  ReferenceError: bodyParser is not defined

Problem Statement

unable to start node js server .


Error Details 

error Log

ERROR DETAILS 

ReferenceError: bodyParser is not defined
    at Object.<anonymous> (F:\abc\study\gitRepo\new\learn\nodejs\expressjs\server.js:13:9)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)



Error Code 

You may have body parser in your code .


var express = require('express');
var bodyParser = require('body-parser');
var app = express();


var users = require('./service/users.js');

app.use(express.static('public'));

//app.get('/', function (req, res) {
  // res.send('Hello World');
//})

app.use('/users'users);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var server = app.listen(8082function () {
   var host = server.address().address;
   var port = server.address().port;

   console.log("Example app listening at http://%s:%s"hostport)
})
  

Solution

body parser should be installed 

Code Changes 

execute the following command 

  
        

> npm install body-parser

         

OUTPUT
npm WARN saveError ENOENT: no such file or directory, open 'F:\abc\study\gitRepo\new\learn\nodejs\expressjs\package.json'  
npm WARN enoent ENOENT: no such file or directory, open 'F:\abc\study\gitRepo\new\learn\nodejs\expressjs\package.json'     
npm WARN expressjs No description
npm WARN expressjs No repository field.
npm WARN expressjs No README data
npm WARN expressjs No license field.

+ body-parser@1.19.0
updated 1 package and audited 316 packages in 3.986s
found 0 vulnerabilities



Thanks for reading . Let me know your thoughts in the comments below    .

ec2-user@ec2 Permission denied