错误:发送后设置标头-帮助我了解原因?

时间:2018-11-25 10:51:08

标签: javascript node.js http express fetch

我不知道将标头发送给客户端后如何设置标头?

代码::在表单提交过程中,发出了ajax请求,响应是返回给客户端的json对象。

我被注释掉了大部分代码,以找出原因,目前,我仅在发出发布请求后才执行一个res.json()。我不知道在什么时候我要重新设置标题/或两次返回响应?

但我收到此错误:

#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>

main (int argc, char *argv[])
{
  int i;
  int err;
  char *buffer;
  int buffer_frames = 128;
  unsigned int rate = 44100;
  snd_pcm_t *capture_handle;
  snd_pcm_hw_params_t *hw_params;
  snd_pcm_format_t format = SND_PCM_FORMAT_FLOAT;
 //   snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;

  if ((err = snd_pcm_open (&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0) {
    fprintf (stderr, "cannot open audio device %s (%s)\n", 
             argv[1],
             snd_strerror (err));
    exit (1);
  }

  fprintf(stdout, "audio interface opened\n");

  if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
    fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
             snd_strerror (err));
    exit (1);
  }

  fprintf(stdout, "hw_params allocated\n");

  if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
    fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
             snd_strerror (err));
    exit (1);
  }

  fprintf(stdout, "hw_params initialized\n");

  if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
    fprintf (stderr, "cannot set access type (%s)\n",
             snd_strerror (err));
    exit (1);
  }

  fprintf(stdout, "hw_params access setted\n");

  if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, format)) < 0) {
    fprintf (stderr, "cannot set sample format (%s)\n",
             snd_strerror (err));
    exit (1);
  }

  fprintf(stdout, "hw_params format setted\n");

  if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
    fprintf (stderr, "cannot set sample rate (%s)\n",
             snd_strerror (err));
    exit (1);
  }

  fprintf(stdout, "hw_params rate setted\n");

  if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
    fprintf (stderr, "cannot set channel count (%s)\n",
             snd_strerror (err));
    exit (1);
  }

  fprintf(stdout, "hw_params channels setted\n");

  if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
    fprintf (stderr, "cannot set parameters (%s)\n",
             snd_strerror (err));
    exit (1);
  }

  fprintf(stdout, "hw_params setted\n");

  snd_pcm_hw_params_free (hw_params);

  fprintf(stdout, "hw_params freed\n");

  if ((err = snd_pcm_prepare (capture_handle)) < 0) {
    fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
             snd_strerror (err));
    exit (1);
  }

  fprintf(stdout, "audio interface prepared\n");

  buffer = malloc(128 * snd_pcm_format_width(format) / 8 * 2);

  fprintf(stdout, "buffer allocated\n");
  FILE *fp = fopen("in.pcm", "a+");
  //for (i = 0; i < 10; ++i) {
  i = 0;
  while(++i) {
    snd_pcm_wait(capture_handle, 1000);
    if ((err = snd_pcm_readi (capture_handle, buffer, buffer_frames)) != buffer_frames) {
      fprintf (stderr, "read from audio interface failed (%s)\n",
               snd_strerror (err));
      exit (1);
    }
    fwrite(buffer, 1, buffer_frames, fp);
    fprintf(stdout, "read %d done\n", i);
  }
  fclose(fp);

  free(buffer);

  fprintf(stdout, "buffer freed\n");

  snd_pcm_close (capture_handle);
  fprintf(stdout, "audio interface closed\n");

  exit (0);
}

index.js

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 (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:170:12)
    at done (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:1004:10)
    at Object.exports.renderFile (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/pug/lib/index.js:412:12)
    at View.exports.__express [as engine] (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/pug/lib/index.js:455:11)
    at View.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/view.js:135:8)
    at tryRender (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/application.js:640:10)
    at Function.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:1008:7)

myscript.js ---前端客户端

var express = require('express');
var router = express.Router();

let users = [];



/* GET home page. */
router.get('/', function(req, res, next) {

  res.render('index', { title: 'Express' });
});

router.post('/', function(req, res, next) {
  // check post valued are true
  if(req.body.fname && req.body.list_name && req.body.list_date) {
    // try {
       addNewUser(req.body);
       res.json({
         "message":"Successfully signed up!",
         "userName": req.body.fname,
         "userDate": req.body.list_date
        });
    // } catch(e){
      // signup failed
      // res.json(res.status(status).json(obj), "Sign up failed...");
    // }
   }
  //  if(req.body.list_item) {
  //    console.log(req.body.list_item);
  //  }
   console.log(body.req);

});
module.exports = router;

function addNewUser(data) {
  try {
    users.push(
      {
        "fname":data.fname,
        "list_name": data.list_name,
        "list_date": data.list_date
      }  
    );  
  } catch(e) {
    console.log("Failed to add user ", e);
      throw e;
  }

}

1 个答案:

答案 0 :(得分:0)

console.log(body.req);,这是错误的。它应该是console.log(req.body);。通常会在程序退出时发生,但堆栈中仍然存在函数回调。这应该可以解决您的问题。如果没有,请共享控制台日志(如果有的话)