簡體   English   中英

Node.js res.setheader不是_respond的函數

[英]Node.js res.setheader is not a function at _respond

為node.js制作輔助函數時出現以下錯誤。 當我不在helpersFunction.js中使用它時,它就可以正常工作。 一個應該如何在Node.js中正確設置助手功能?

TypeError:res.setheader不是Object.module.exports.success(/ Users / camdixon / Sites / node / node_api2 /helpsFunctions.js:15:3)位於/Users/camdixon/Sites/node/node_api2/routes.js:14:13位於Layer.handle [作為handle_request](/ Users / camdixon / Sites / node / node_api2 / node_modules /在下一個(/Users/camdixon/Sites/node/node_api2/node_modules/express/lib/router/route.js:137:13)Express / lib / router / layer.js:95:5)

helperFunctions.js文件

function _respond(res, next, status, data, http_code) {
  var response = {
    'status': status,
    'data': data
  };

  res.setheader('content-type', 'application/json');
  res.writeHead(http_code);
  res.end(JSON.stringify(response));
  return next();
}

module.exports.success = function(res, next, data) {
  _respond(res, next, 'OK', data, 200);
}

module.exports.failure = function(res, next, data, http_code) {
  _respond(res, next, 'Error', data, http_code);
}

我的Routes.js文件

var helpers = require('./helperFunctions.js');

module.exports = function (app) {

  var list = {}

  app.post('/list/create', function(req, res, next) {
    list = req.body.email_address;
    console.log(list);

    helpers.success(res, next, list);
  });

};

如果查看stacktrace,您會發現您有錯別字。 該方法稱為setHeader而不是setheader。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM