@zy445566
报错信息:
/mnt/www/pgyer/nodejs-middle/server/api/frontEnd/forword/index.js:163:40 代码就是下面的 return res.redirect(_forword);
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 (/mnt/www/pgyer/nodejs-middle/node_modules/express/lib/response.js:771:10) at ServerResponse.location (/mnt/www/pgyer/nodejs-middle/node_modules/express/lib/response.js:888:15)
at ServerResponse.redirect (/mnt/www/pgyer/nodejs-middle/node_modules/express/lib/response.js:926:18)
at Request.redirect [as _callback] (/mnt/www/pgyer/nodejs-middle/server/api/frontEnd/forword/index.js:163:40)
at Request.self.callback (/mnt/www/pgyer/nodejs-middle/node_modules/request/request.js:185:22)
at Request.emit (events.js:198:13) at Request. (/mnt/www/pgyer/nodejs-middle/node_modules/request/request.js:1161:10)
at Request.emit (events.js:198:13) at IncomingMessage. (/mnt/www/pgyer/nodejs-middle/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:286:20) at IncomingMessage.emit (events.js:203:15) at endReadableNT (_stream_readable.js:1129:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
源码:
这是/redis/index.js
const config = require('../../config/index');
var redis = require("redis"),
client = redis.createClient({
...config.redis
});
client.on("error", function (err) {
console.error("Errorssss " + JSON.stringify(err));
});
client.on('ready', function (res) {
console.log('redis is ready,Go!');
});
function clientCache() { }
let text = async (key) => {
let doc = await new Promise((resolve, reject) => {
client.get(key, function (err, res) {
if (err) {
console.log(err);
reject(err);
return;
}
return resolve(res);
});
}).catch((e) => {
debugger;
console.error(e);
});
return doc ? JSON.parse(doc) : null;
};
clientCache.set = function (key, value) {
value = JSON.stringify(value);
return client.set(key, value, function (err) {
if (err) {
console.error(err);
client = redis.createClient({
...config.redis
});
return clientCache.set(key, value);
}
});
};
clientCache.get = async (key) => {
return await text(key);
};
clientCache.expire = function (key, time) {
return client.expire(key, time);
};
module.exports = exports = clientCache;
这是/forword/index.js
const Express = require('express');
const router = Express.Router();
const {
config,
SelfCookie,
codeState,
result,
log,
token,
HttpApi,
Client,
DEBUGGER,
ResultFactory
} = require('../../../namespaceMain');
const request = require('request');
const md5 = require('md5');
const client = require("../../../common/redis");
router.get('/:customerid/:accode/:timestamp/:sign', async function (req, res, next) {
try {
debugger;
const _result = '网络出了个小差……';
console.log(`/:customerid/:accode/:timestamp/:sign ${JSON.stringify(req.params)}`);
var _customerid = req.params.customerid,
_accode = req.params.accode,
__header_sign = req.params.sign,
__timestamp = req.params.timestamp,
__sign = md5(_customerid + _accode + config.ZhshToNodejsToken[_customerid] + __timestamp),
_param_notnull = (_customerid && _accode && __header_sign),
_b_tokenisok = (__sign === __header_sign),
_code_sign = `${_customerid}_${_accode}_${__timestamp}_${__sign}`;
log.logger.info(`/:customerid/:accode/:timestamp/:sign /${_customerid}/${_code_sign}/${__timestamp}/${__sign} SessionId: ${req.session.id} GetParameter: \n `);
console.log(`/forword/index - /${_customerid}/${_code_sign}/${__timestamp}/${__sign} SessionId: ${req.session.id}`);
if (!config.EnableZhshToNodejsToken) {
_param_notnull = (_customerid && _accode);
_b_tokenisok = true;
}
const key = `forword:${_customerid}:${_accode}`;
const key_info = `forword:${_customerid}:actinfo:${_code_sign}`;
if (!_param_notnull) {
log.logger.info('/forword/index 参数为空或值错误' + "\n");
console.log('/forword/index 参数为空或值错误' + "\n");
return res.send(_result);
}
if (!_b_tokenisok) {
log.logger.info('/forword/index 签名校验不成功' + "\n");
console.log('/forword/index 签名校验不成功' + "\n");
return res.send(_result);
}
console.time('/forword/index');
var reply = await client.get(`${key}`);
if (reply) {
console.log(`/forword/index - 已缓存 ${JSON.stringify(reply)}`);
console.timeEnd('/forword/index');
return res.redirect(reply);
} else {
var _f = {
customerid: _customerid,
accode: _code_sign
}
log.logger.info('/forword/index SessionId:' + req.session.id + ' Begin:', JSON.stringify(_f) + "\n");
request({
url: HttpApi.Home.Index,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
"Authorization": req.session.Authorization
},
form: _f
}, function (error, response, body) {
var str_body = JSON.stringify(body);
log.logger.info('/forword/index SessionId:' + req.session.id + ' End:', str_body + "\n");
console.timeEnd('/forword/index');
console.log(`/forword/index - Response:${str_body}`);
if (body.success) {
if (body.result && body.result.module && body.result.module.template && body.result.module.template.url && body.result.module.id) {
var _type = body.result.module.template.url;
var _moduleid = body.result.module.id;
var _forword = `${config.FrontendToDomain}${_type}${_customerid}/${_moduleid}/${_code_sign}`;
client.set(`${key}`, _forword, 'EX', (60 * 1 * 30));
client.set(`${key_info}`, str_body, 'EX', (60 * 1 * 30));
return res.redirect(_forword);
} else {
log.logger.info('/forword/index 模块未配置成功!' + "\n");
console.log('/forword/index 模块未配置成功!' + "\n");
return res.send(_result);
}
} else {
log.logger.info('/forword/index 返回 false' + "\n");
console.log('/forword/index 返回 false' + "\n");
return res.send(_result);
}
});
}
} catch (e) {
console.error('00000000000000000000000000000000');
console.error(JSON.stringify(e));
}
});
module.exports = router;