0

I want to precompile some Jade templates mostly based on server configuration (so result is static html) upon server startup. Then for every client request I want to serve this content as fast as possible.

There is probably several options, but I am not sure what would most efficient.

  1. Save in files and stream on every request
  2. Save in Redis database and stream (not sure how yet) on every request
  3. Store everything in memory as strings and send on every request

There is maybe more options I don't know about. I was even thinking about creating stream, that can be reused all over again for every request, but it doesn't seems to be possible.

I am not looking for ultimate super solution here, just maybe if you can share your experience on this topic.

FredyC
  • 3,999
  • 4
  • 30
  • 38
  • Are you open to adding nginx or httpd to your system, or do you want to have it be all in node? – John Zwinck Apr 06 '14 at 10:31
  • Well current idea is to have this NodeJS serve just "index.html" and all other content will go from CDN or similar. This NodeJS should be primarily used for socket communication and few REST. So it needs to stay responsive as much as possible. I don't have experience with nginx or httpd, how would that help ? – FredyC Apr 06 '14 at 10:42
  • You could generate the static files once in a while and let nginx serve them. It will be about as fast as your network connection allows it to be, and should take less resources on the server than using Node itself for each request. – John Zwinck Apr 06 '14 at 10:43
  • I suppose that nginx would need to be running on different port, aka. different subdomain *(app.domain.com, content.domain.com)* and there goes troubles with XHR cross domain that I need to avoid. That's why I wanted to serve this initial "index" through NodeJS so it comes from the domain that will be used for communication. – FredyC Apr 06 '14 at 10:45

2 Answers2

1

You should put nginx in front of node.js. Then nginx can serve semi-static pages all by itself with high efficiency, and it can forward dynamic page requests to node.js. For more details on how to set that up, see: Node.js + Nginx - What now?

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I am not sure this will help me. I should have mentioned, that there will be at least two "index.html", one from guests that needs to sign in and other for signed people. Since nginx doesn't know about authentication, I need NodeJS to decide which page to serve. Sorry for misleading information. – FredyC Apr 06 '14 at 10:54
  • nginx can know about authentication. – John Zwinck Apr 06 '14 at 10:55
  • Not sure how, I am not using sessions or cookies... If it's just proxy, how could it decide based on NodeJS logic ? – FredyC Apr 06 '14 at 10:56
  • Maybe this will help? https://groups.google.com/forum/#!topic/python-tornado/sgadmx8Hd_s – John Zwinck Apr 06 '14 at 11:01
  • That looks really interesting, thanks, I will give it a try. (I will wait for other people to possibly respond before marking this like answer) – FredyC Apr 06 '14 at 11:05
1

Just adding to answer by John Zwinck You can use memcache additionally if you think for some reason setting static files (servable by nginx) wont work for you,or if the data is timed/needs to be updated frequently/needs to expire after some time-interval. Nginx has inbuild memcache module. you can have it query from memcache for specific urls.

Shrey
  • 569
  • 5
  • 14
  • Well, what is the advantage of memcache over Redis ? I wasn't working with memcache before, but looking at [elbart/node-memcache](https://github.com/elbart/node-memcache) it seems, that API is very similar. – FredyC Apr 06 '14 at 18:30