title: npm server closes when making a post request
body: <p>I am following an course in mean stack and when I tried the authentication part by using a jwt token system and npm server started to close down each time I make a request. when I try to make a post request and store data in the db, data gets stored but right after that npm server closes.</p>
  <p>code does not seem to be having any errors. works fine data gets stored and everything but npm server closes right after sending and request</p>
  <p>message.js</p>
  <pre><code>router.post('/', function(req, res, next) {
  
      var decoded = jwt.decode(req.query.token)
      User.findById(decoded.user._id, function(err, user){
          if (err){
              return res.status(500).json({
                  title:'An error ocured',
                  error:err
              });                
          }
          var message =new Message({
              content:req.body.content,
              user: user        
          });
          message.save(function(err,result){
              if(err){
                  return res.status(500).json({
                      title:'An error ocured',
                      error:err
                  });
              }
              user.messages.push(result);
              user.save();
  
              res.status(201).json({
                  message:'saved message',
                  obj:result
              })
          })
   })
  </code></pre>
  <p>message.service.ts</p>
  <pre><code>addMessage(message:Message){
      
      const body = JSON.stringify(message);
      const headers = new Headers({'Content-Type':'application/json'});
      const token = localStorage.getItem('token')
          ? '?token=' + localStorage.getItem('token')
          :'';
      return this.http.post('http://localhost:3000/message'+ token, body, {headers:headers})
          .map ((response : Response) =&gt;  {
              const result=response.json()
              const message= new Message(result.obj.content,'Dummy',result.obj._id,null)
              this.messages.push(message)
              return message;
              
          } )
          
          .catch((error: Response) =&gt; Observable.throw(error.json()));
  }
  </code></pre>
  <p>package.json</p>
  <pre><code>{
     &quot;name&quot;: &quot;nodejs-angular2&quot;,
     &quot;version&quot;: &quot;1.0.0&quot;,
     &quot;private&quot;: true,
     &quot;scripts&quot;: {
     &quot;start&quot;: &quot;node ./bin/www&quot;,
     &quot;build&quot;: &quot;del-cli public/js/app &amp;&amp; webpack --config 
      webpack.config.dev.js --progress --profile --watch&quot;,
      &quot;build:prod&quot;: &quot;del-cli public/js/app &amp;&amp; ngc -p tsconfig.aot.json &amp;&amp; 
      ngc -p tsconfig.aot.json &amp;&amp; webpack --config webpack.config.prod.js -- 
      progress --profile --bail &amp;&amp; del-cli 'assets/app/**/*.js' 
       'assets/app/**/*.ngsummary.json' 'assets/app/**/*.ngstyle.*'&quot;
       },
  &quot;dependencies&quot;: {
  &quot;@angular/animations&quot;: &quot;^5.0.0&quot;,
  &quot;@angular/common&quot;: &quot;^5.0.0&quot;,
  &quot;@angular/compiler&quot;: &quot;^5.0.0&quot;,
  &quot;@angular/compiler-cli&quot;: &quot;^5.0.0&quot;,
  &quot;@angular/core&quot;: &quot;^5.0.0&quot;,
  &quot;@angular/forms&quot;: &quot;^5.0.0&quot;,
  &quot;@angular/http&quot;: &quot;^5.0.0&quot;,
  &quot;@angular/platform-browser&quot;: &quot;^5.0.0&quot;,
  &quot;@angular/platform-browser-dynamic&quot;: &quot;^5.0.0&quot;,
  &quot;@angular/platform-server&quot;: &quot;^5.0.0&quot;,
  &quot;@angular/router&quot;: &quot;^5.0.0&quot;,
  &quot;@angular/upgrade&quot;: &quot;^5.0.0&quot;,
  &quot;bcryptjs&quot;: &quot;^2.4.3&quot;,
  &quot;body-parser&quot;: &quot;~1.15.2&quot;,
  &quot;cookie-parser&quot;: &quot;~1.4.3&quot;,
  &quot;core-js&quot;: &quot;^2.4.1&quot;,
  &quot;debug&quot;: &quot;~2.2.0&quot;,
  &quot;express&quot;: &quot;~4.14.0&quot;,
  &quot;hbs&quot;: &quot;~3.1.0&quot;,
  &quot;interpret&quot;: &quot;^1.2.0&quot;,
  &quot;jsonwebtoken&quot;: &quot;^8.5.1&quot;,
  &quot;mongoose&quot;: &quot;^5.5.5&quot;,
  &quot;mongoose-unique-validator&quot;: &quot;^2.0.3&quot;,
  &quot;morgan&quot;: &quot;~1.6.1&quot;,
  &quot;reflect-metadata&quot;: &quot;^0.1.3&quot;,
  &quot;rxjs&quot;: &quot;^5.5.0&quot;,
  &quot;serve-favicon&quot;: &quot;~2.3.0&quot;,
  &quot;zone.js&quot;: &quot;^0.8.5&quot;
  },
   &quot;devDependencies&quot;: {
  &quot;@ngtools/webpack&quot;: &quot;^1.8.0&quot;,
  &quot;@types/core-js&quot;: &quot;0.9.36&quot;,
  &quot;@types/node&quot;: &quot;^6.0.45&quot;,
  &quot;angular-router-loader&quot;: &quot;^0.5.0&quot;,
  &quot;angular2-template-loader&quot;: &quot;^0.5.0&quot;,
  &quot;awesome-typescript-loader&quot;: &quot;^3.1.2&quot;,
  &quot;del-cli&quot;: &quot;^0.2.0&quot;,
  &quot;html-loader&quot;: &quot;^0.4.4&quot;,
  &quot;raw-loader&quot;: &quot;^0.5.1&quot;,
  &quot;ts-loader&quot;: &quot;^2.0.3&quot;,
  &quot;typescript&quot;: &quot;^2.4.2&quot;,
  &quot;webpack&quot;: &quot;^2.2.1&quot;,
  &quot;webpack-merge&quot;: &quot;^4.1.0&quot;
     }
    }
  </code></pre>
  <p>ERROR MESSAGES</p>
  <blockquote>
  <p>ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0, total: 0, type: &quot;error&quot;, …}</p>
  <p>zone.js?fad3:3243 POST <a href="http://localhost:3000/message?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Im1lc3NhZ2VzIjpbXSwiX2lkIjoiNWNmMmUzYjRjNTdiMDQ0NDE4YWRjZmY5IiwiZmlyc3ROYW1lIjoic2F0aGlyYSIsImxhc3ROYW1lIjoicGFzYW4iLCJwYXNzd29yZCI6IiQyYSQxMCR0b3pBNm1kU0dnT04xNDBmV3BmQUZ1dDcvZTQyQjR5Y1NFRUxvWXNxbWlwaEJLaUtaWW1BaSIsImVtYWlsIjoic2F0aGlyYUBwYXNhbi5jb20iLCJfX3YiOjB9LCJpYXQiOjE1NTk0MjI3MDAsImV4cCI6MTU1OTQyOTkwMH0.gKHQmIszqLyQrR3g-PKLjqyJz9MFEgU4hKhTT6boFMA" rel="nofollow noreferrer">http://localhost:3000/message?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Im1lc3NhZ2VzIjpbXSwiX2lkIjoiNWNmMmUzYjRjNTdiMDQ0NDE4YWRjZmY5IiwiZmlyc3ROYW1lIjoic2F0aGlyYSIsImxhc3ROYW1lIjoicGFzYW4iLCJwYXNzd29yZCI6IiQyYSQxMCR0b3pBNm1kU0dnT04xNDBmV3BmQUZ1dDcvZTQyQjR5Y1NFRUxvWXNxbWlwaEJLaUtaWW1BaSIsImVtYWlsIjoic2F0aGlyYUBwYXNhbi5jb20iLCJfX3YiOjB9LCJpYXQiOjE1NTk0MjI3MDAsImV4cCI6MTU1OTQyOTkwMH0.gKHQmIszqLyQrR3g-PKLjqyJz9MFEgU4hKhTT6boFMA</a> net::ERR_CONNECTION_RESET</p>
  </blockquote>