1

如何通过 cloudfoundry 使用 express 提供静态文件?我试图使用以下代码:

app.use(express.static(__dirname+'/www'));

但是当我访问特定的 url 时,我遇到了一些奇怪的异常:

TypeError: Object #<SendStream> has no method 'on'
    at Object.static [as handle] (/var/vcap/data/dea/apps/FeedBack-0-c812c99aa3818946dd0bcf5538e2aecd/app/node_modules/express/node_modules/connect/lib/middleware/static.js:75:8)
    at next (/var/vcap/data/dea/apps/FeedBack-0-c812c99aa3818946dd0bcf5538e2aecd/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at Object.expressInit [as handle] (/var/vcap/data/dea/apps/FeedBack-0-c812c99aa3818946dd0bcf5538e2aecd/app/node_modules/express/lib/middleware.js:31:5)
    at next (/var/vcap/data/dea/apps/FeedBack-0-c812c99aa3818946dd0bcf5538e2aecd/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at Object.query [as handle] (/var/vcap/data/dea/apps/FeedBack-0-c812c99aa3818946dd0bcf5538e2aecd/app/node_modules/express/node_modules/connect/lib/middleware/query.js:44:5)
    at next (/var/vcap/data/dea/apps/FeedBack-0-c812c99aa3818946dd0bcf5538e2aecd/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at Function.handle (/var/vcap/data/dea/apps/FeedBack-0-c812c99aa3818946dd0bcf5538e2aecd/app/node_modules/express/node_modules/connect/lib/proto.js:198:3)
    at Server.app (/var/vcap/data/dea/apps/FeedBack-0-c812c99aa3818946dd0bcf5538e2aecd/app/node_modules/express/node_modules/connect/lib/connect.js:66:31)
    at Server.emit (events.js:67:17)
    at HTTPParser.onIncoming (http.js:1134:12)
4

2 回答 2

4

我也弄错了,我找到了答案;你可以这样做 :

第一 vmc delete yourapp

第二vmc push yourapp --runtime node08

也许您在第一次将应用程序推送到云时选择了错误的 Node 运行时版本(node04、node06),并且服务器会记住您的选择。

于 2012-08-14T10:17:08.283 回答
2

这应该可以,如果您创建一个模板 express 应用程序,它被配置为从 ./public 文件夹提供静态资产。确保您的配置是这样的;

var app = express();

app.configure(function(){
  app.set('port', process.env.VCAP_APP_PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

如果您愿意,我可以尝试自己部署您的源代码,看看是否可以让它工作。

于 2012-08-08T13:33:22.530 回答