如何在Openshift NodeJS应用程序上为静态资产提供服务

时间:2022-11-15 03:04:13

I am working on my NodeJS app hosted by Openshift. Everything works fine right now but I want to speed things up by serving static files (html, css, js) from a web server rather than doing it from Express. I've read somewhere that Node cartridges do not have an Apache server running, and thus no .htaccess file from where I can configure Apache to send my files.

我正在开发由Openshift托管的NodeJS应用程序。现在一切正常但我希望通过从Web服务器提供静态文件(html,css,js)而不是从Express执行来加快速度。我已经在某处看到Node盒没有运行Apache服务器,因此没有.htaccess文件可以配置Apache来发送我的文件。

How can I serve my static files from a web server like Apache or Nginx from my NodeJS app on Openshift?

如何从Openshift上的NodeJS应用程序中提供来自Apache或Nginx等Web服务器的静态文件?

2 个解决方案

#1


This may suit your needs. Bare as it gets static server...

这可能适合您的需求。裸露,因为它获得静态服务器...

var finalhandler = require('finalhandler')
var http = require('http')
var serveStatic = require('serve-static')

// Serve up public/ftp folder
var serve = serveStatic('public/', {'index': ['index.html']})

// Create server
var server = http.createServer(function(req, res){
    var done = finalhandler(req, res)
    serve(req, res, done)
})

// Listen
server.listen(process.env.PORT || 3000);

#2


It's been a while since the original question was posted, but maybe this will help other people facing the same issue. Have a look at this custom OpenShift cartridge here: https://github.com/gsterjov/openshift-nginx-cartridge

自原始问题发布以来已经有一段时间了,但也许这将有助于其他人面临同样的问题。在这里看看这个自定义OpenShift盒式磁带:https://github.com/gsterjov/openshift-nginx-cartridge

I haven't tested it personally, but I've built other custom cartridges and I know the OpenShift platform is quite flexible if you're proficient enough with the shell, so if the above cartridge doesn't suit your needs you can easily fork it and tweak it as you see fit.

我没有亲自测试过,但是我已经构建了其他自定义墨盒,我知道如果你对外壳足够熟练,OpenShift平台是非常灵活的,所以如果上面的墨盒不适合你的需要你就可以轻松分叉它,并根据你的需要调整它。

Personally I'm almost always serving static assets from Node.js. The built-in static server in Express.js got so much better lately and there's also st if you need more control over caching / etags.

就个人而言,我几乎总是从Node.js提供静态资产。 Express.js中的内置静态服务器最近变得更好,如果您需要更多地控制缓存/ etags,那么也是如此。

Also, I've recently came across this interesting CDN-like alternative to "classic" hosting for static assets: http://surge.sh. I can imagine it would be fairly trivial to implement a gulp/grunt scenario for publishing your static assets on surge on deployment...

此外,我最近遇到了这种有趣的CDN式替代静态资产的“经典”托管:http://surge.sh。我可以想象,实施一个gulp / grunt场景以便在部署激增时发布静态资产是相当微不足道的......

#1


This may suit your needs. Bare as it gets static server...

这可能适合您的需求。裸露,因为它获得静态服务器...

var finalhandler = require('finalhandler')
var http = require('http')
var serveStatic = require('serve-static')

// Serve up public/ftp folder
var serve = serveStatic('public/', {'index': ['index.html']})

// Create server
var server = http.createServer(function(req, res){
    var done = finalhandler(req, res)
    serve(req, res, done)
})

// Listen
server.listen(process.env.PORT || 3000);

#2


It's been a while since the original question was posted, but maybe this will help other people facing the same issue. Have a look at this custom OpenShift cartridge here: https://github.com/gsterjov/openshift-nginx-cartridge

自原始问题发布以来已经有一段时间了,但也许这将有助于其他人面临同样的问题。在这里看看这个自定义OpenShift盒式磁带:https://github.com/gsterjov/openshift-nginx-cartridge

I haven't tested it personally, but I've built other custom cartridges and I know the OpenShift platform is quite flexible if you're proficient enough with the shell, so if the above cartridge doesn't suit your needs you can easily fork it and tweak it as you see fit.

我没有亲自测试过,但是我已经构建了其他自定义墨盒,我知道如果你对外壳足够熟练,OpenShift平台是非常灵活的,所以如果上面的墨盒不适合你的需要你就可以轻松分叉它,并根据你的需要调整它。

Personally I'm almost always serving static assets from Node.js. The built-in static server in Express.js got so much better lately and there's also st if you need more control over caching / etags.

就个人而言,我几乎总是从Node.js提供静态资产。 Express.js中的内置静态服务器最近变得更好,如果您需要更多地控制缓存/ etags,那么也是如此。

Also, I've recently came across this interesting CDN-like alternative to "classic" hosting for static assets: http://surge.sh. I can imagine it would be fairly trivial to implement a gulp/grunt scenario for publishing your static assets on surge on deployment...

此外,我最近遇到了这种有趣的CDN式替代静态资产的“经典”托管:http://surge.sh。我可以想象,实施一个gulp / grunt场景以便在部署激增时发布静态资产是相当微不足道的......