How do I serve static file with express over cloudfoundry?

时间秒杀一切 提交于 2019-12-24 20:35:21

问题


How do I serve static files with express over cloudfoundry? I was trying to use the following code:

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

but I am getting some bizarre exception when I am accessing the specific 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)

回答1:


I'v got this wrong too,and i found the answer; you can do this :

1st vmc delete yourapp

2nd vmc push yourapp --runtime node08

maybe you choose the wrong varsion of the Node runtime (node04,node06) at the 1st time you push the app to cloud ,and the server remember your choice.




回答2:


That should work, if you create a template express application it is configured to serve static assets from the ./public folder. Make sure your configuration is something like this;

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'));
});

If you would like, I can try and deploy your source myself and see if I can get it to work.



来源:https://stackoverflow.com/questions/11865214/how-do-i-serve-static-file-with-express-over-cloudfoundry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!