2

尝试使用 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/');
4

3 回答 3

2

我的问题是我需要删除:

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

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

于 2012-07-26T21:06:03.743 回答
0

你可以通过两种方式做到这一点。在 app.get(/ ..) 上方添加另一条路线,如下所示:

// global controller
app.get('/*',function(req,res,next){
    res.header('Content-Type': 'text/plain' , 0 );
    next(); // http://expressjs.com/guide.html#passing-route control
});

或者您可以在现有路由中添加中间件功能

addToHeader = function (req, res, next) {
  console.log("add to header called ... " + req.url);
  res.header('Content-Type': 'text/plain', '0');
  next();
}
and then change your routes to sth like this:

app.get('/', addToHeader, function(req,res){
  var stuff = { 'title': 'blah' };
  res.render('mytemplate',stuff);
});
于 2012-07-26T19:32:47.023 回答
0

使用 res.render() 时,不需要手动处理响应(res.end()、res.writeHead() 等)。

于 2012-09-06T10:28:26.730 回答