gpt4 book ai didi

Node.js、Express、Jade - 错误 : Can't set headers after they are sent

转载 作者:太空宇宙 更新时间:2023-11-03 22:03:02 24 4
gpt4 key购买 nike

尝试使用 Jade 生成 HTML 页面时出现以下错误。还有其他人经历过这个问题吗?我在下面指出了导致错误的具体位置。

错误

Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:644:11)
at ServerResponse.res.setHeader (/Users/dereklo/node_modules/express/node_modules/connect/lib/patch.js:59:22)
at ServerResponse.res.set.res.header (/Users/dereklo/node_modules/express/lib/response.js:475:10)
at ServerResponse.res.contentType.res.type (/Users/dereklo/node_modules/express/lib/response.js:350:15)
at ServerResponse.res.send (/Users/dereklo/node_modules/express/lib/response.js:111:14)
at res.render.fn (/Users/dereklo/node_modules/express/lib/response.js:672:10)
at Object.exports.render (/Users/dereklo/node_modules/jade/lib/jade.js:216:5)

Node.js/Express/Jade 源代码

var http = require('http'),
express = require('express'),
jade = require('jade'),
url = require('url'),
jsdom = require('jsdom'),
child_proc = require('child_process'),
w,
h,
scripts = ["/Users/dereklo/Node/pie/d3.min.js",
"/Users/dereklo/Node/pie/d3.v2.js",
"/Users/dereklo/Node/pie/d3.layout.min.js",
"/Users/dereklo/Node/pie/RadialScriptMobileServ.js",
"/Users/dereklo/Node/pie/RadialScriptMobile.js",
"/Users/dereklo/Node/pie/canvg.js"];

//scripts = ["./d3.v2.js",
// "./d3.layout.min.js",
// "./pie.js"]

htmlStub = '<!DOCTYPE html><div id="RadialScriptMobileServ"></div>',
querystring = require("querystring"),
fs = require("fs"),
formidable = require("formidable"),
path = require('path'),
request = require('request')
svgsrc = '';



//create an app server
var app = require('express').createServer();
//set path to the views (template) directory
app.set('views', __dirname + '/views');
//set path to static files
app.use(express.static(__dirname + '/../public'));
//handle GET requests on /
app.get('/', function(req, res){



res.writeHead(200, {'Content-Type': 'text/plain'});

w = (url.parse(req.url, true).query['width']);
h = (url.parse(req.url, true).query['height']);

console.log("width: ",w);
console.log("height: ",h);

request("http://dcaps-staging.media.mit.edu:8080/api/reality_analysis_service/get_reality_analysis_data?document_key=radialData&bearer_token=8e2f9e3129", function (err, result, json) {
json = JSON.parse(json);

console.log("my radial data: ",json.radialData.data);


jsdom.env({features:{QuerySelector:true}, html:htmlStub, scripts:scripts, done:function(errors, window) {

svgsrc = window.insertRadial("#RadialScriptMobileServ",w,h,json).innerHTML;
console.log(svgsrc);
res.render('/Users/dereklo/Node/pie/Jade_radial.jade', {pageTitle: 'Franz Enzenhofer'}); // ERROR OCCURRING HERE!


// res.write(svgsrc);
res.end();

}})
})

});
//listen on localhost:3000
app.listen(3000);

//console.log('Pie SVG server running at http://127.0.0.1:3000/');

最佳答案

我的问题是我需要删除:

res.writeHead(200, {'Content-Type': 'text/plain'});

现在可以完美运行了。我希望这能帮助其他偶然发现这篇文章的人......

关于Node.js、Express、Jade - 错误 : Can't set headers after they are sent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11676556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com