multisite with node.js - javascript

Anyone know if there's a combination of packages that can be used together to create a multisite setup with node.js?
A multisite setup would have one code base, possibly one server, serving different tlds from different databases.
Thanks.

Maybe this is what you are looking for?
https://github.com/visionmedia/express/blob/master/examples/vhost/index.js

An alternative is that you could simply run a number of sites, each on their own ports and use https://github.com/substack/bouncy to route incoming requests to the appropriate site.
You can choose how your sites are started as well. You can start them up as separate executables, or you can create something a bit more dynamic using http://nodejs.org/docs/latest/api/child_processes.html#child_process.fork. This would allow you to have a single executable with any number of means of starting up sites, bringing them up or down, gathering statistics from them, whatever.
If you're looking for a straight-up CMS, check out http://calip.so/

Related

serving javascript libraries from CDN vs combining them as one and serving as a single http connection

This is a question about improving the page-speed.
When you are given the following scenarios, which one to go with?
scenario 1
you got 10 famous js libraries in the head & all being served from respected CDN repositories. ( this means possibly 10-HTTP-connections for some people)
scenario 2
you choose to combine those libraries into one single file on your own and serve them as a single file under 1-HTTP-connection
I incline towards the first one simply due to one reason:
Because those 10 libraries are the "famous" ones, the chances are they may already be in the majority of the users' browsers' cache, avoiding all 10 connections.
Does anyone say anything about this?
What I'm also curious is that when Google evals this page, would it eval assuming those 10 HTTP connections as 10, or would it factor in the fact that this is jQuery so I'm not going to use it against you!
Seems like concatenating them all into one would be a big load, even from your own server. You couldn't delay load one of them (via webpack, for instance) - they'd all have to be loaded up front, no matter what.
Agreed, loading them separately could take advantage of any caching (assuming they're the same versions.)
As a point of interest: HTTP2 addresses the separate files/requests problem with the server push paradigm - the idea is to use a single connection to push known files without the client having to request them, individually.
Don't know if it would work for CDN's -- might have to self-serve them to take advantage.

How do I create a game with Node.js that has multiple servers but only one URL?

I'm relatively new to node.js and I'm trying to make a game that only uses one accessible URL but has multiple pages. In my game I'm going to be using the socket.io and express modules. I really want to have one URL that the client will access which will give them the title screen and then the menu page. There will be buttons on this page which will send the client to different servers that run games. The only way that I can think of how to do this is to have multiple servers running on different pages and have the buttons contain links. I know that it is possible to have multiple servers running on the same URL. One example of this is the diep.io online game. Is it possible to accomplish this with node.js? If so, how would I go about making this happen? Should I be using ports to create these pages?
I believe what you're talking about is referred to as "horizontal scaling" where instead of having one beefy server you have many lower-end servers that run the same code.
Or since you're using Socket.io there's a concept of "rooms" that you can use.
My memory on both of these topics is a bit cloudy so I can't help much more, but you should be able to Google them now.
This channel has tutorials on JS / Node game dev:
https://www.youtube.com/user/RainingChain/featured

Alternative to executing grep while on a shared web-hosting plan

Application:
I wish to publish a web-application that takes input strings, searches for the string in about 5,000 plain-text files and returns the name of the files with matches. Each text file is about 4MB (uncompressed).
Problem:
In PHP, I could use exec(grep -l pattern dir/* ) and get the job done. However, for cost reasons, I would go for a shared web-hosting plan which normally do not allow for executing programs.
Could you please suggest any alternative to grep for web environment?
I have understood following so far:
A binary program file for any grep-alternative (e.g sift) could work. However, the problem of executing on a shared server would remain.
PHP function preg_match is inappropriate considering a large number of files and their size.
I am open to implementations of grep-like function in other languages (e.g perl or javascript). However, I am not sure if the performance would be comparable to grep and whether the problem of execution would still remain.
I have tried looking for different web-hosting providers and understood that a virtual-private server (VPS) might be the solution. However, the price for a VPS plan by all hosting providers I have come across is unaffordable.
Any solutions or guidance for this problem?
Possible solutions depend on what your hosting provider offers and your budget.
Will you have a RDBMS available? You could then use full texts search which many offers. If not you could use SQLite, which has full texts search support.
If you have to stick to low tech solutions, than the PHP solution linked on the right might work for you.
Perl has a File::Find module, which you could use.

How to use fonts in the node.js backend?

Background:
I am building a node.js-based Web app that needs to make use of various fonts. But it only needs to do so in the backend since the results will be delivered as an image. Consequently, the client/browser does not need access to the fonts at all in my case.
Question:
I will try to formulate the question as little subjective as possible:
What are the typical options to provide a node.js backend with a large collection of fonts?
The options I came up with so far are:
Does one install these hundreds or thousands of fonts in the operating system of the (in my case: Ubuntu) server?
Does one somehow serve the fonts from a cloud storage such as S3 or (online) database such as a Mongo DB server?
Does one use a local file system to store the fonts and retrieve them?
...other options
I am currently leaning towards Option 1 because this is the way a layman like me does it on a local machine.
Without starting a discussion here, where could I find resources discussing the (dis-)advantages of the different options?
EDIT:
Thank you for all the responses.
Thanks to these, I noticed that I need to clarify something. I need the fonts to be used in SVG processing libraries such as p5.js, paper.js, raphael.js. So I need to make the fonts available to these libraries that are run on node.js.
The key to your question is
hundreds or thousands of fonts
Until I took that in there is no real difference between your methods. But if that number is correct (kind of mind-boggling though) I would:
not install them in the OS. What happens if you move servers without an image? Or move OS?
Local File system would be a sane way of doing it, though you would need to keep track manually of all the file names and paths for your code.
MongoDB - store file names+paths in the collection..and store the actual fonts in your system.
In advent of moving servers you would have to pick up the directory where all the actual files are stored and the DB where you hold the file name+paths.
If you want you can place it all in a MongoDB but then that file would also be huge, I assume - that is up to you.
Choice #3 is probably what I would do in such a case.
If you have a decent enough server setup (e.g. a VPS or some other VM solution where you control what's installed) then another option you might want to consider is to do this job "out of node". For instance, in one of my projects where I need to build 175+ as-perfect-as-can-be maths statements, I offload that work to XeLaTeX instead:
I run a node script that takes the input text and builds a small but complete .tex file
I then tell node to call "xelatex theFileIJustMade.tex" which yields a pdf
I then tell node to call "pdfcrop" on that pdf, to remove the margins
I then tell node to call "pdf2svg", which is a free and amazingly effective utility
Then as a final step mostly to conserve space and bandwidth, I use "svgo" which is a nodejs based svg optimizer that can run either as normal script code, or as CLI utility.
(some more details on that here, with concrete code here)
Of course, depending on how responsive a system you need, you can do entirely without steps 3 and 5. There is a limit to how fast we can run, but as a server-side task there should never be the expectation of real-time responsiveness.
This is a good example of remembering that your server runs inside a larger operating system that might also offer tools that can do the job. While you're using Node, and the obvious choice is a Node solution, Node is also a general purpose programming language and can call anything else through spawn and exec, much like python, php, java, C#, etc. As such, it's sometimes worth thinking about whether there might be another tool that is even better suited for your needs, especially when you're thinking about doing a highly specialized job like typesetting a string to SVG.
In this case, LaTeX was specifically created to typeset text from the command line, and XeLaTeX was created to do that with full Unicode awareness and clean, easy access to fonts both from file and from the system, with full OpenType feature control, so would certainly qualify as just as worthwhile a candidate as any node-specific solution might be.
As for the tools used: XeLaTeX and pdfcrop come with TeX Live (installed using whatever package manager your OS uses, or through MiKTeX on Windows, but I suspect your server doesn't run on windows) pdf2svg is freely available on github, and svgo is available from npm)

Simple Audio Sequencer (save/open feature)

I have a simple audio sequencer application using javascript and HTML5. The goal of the application is to let users make a beat and save it so other people can open it up, add on to it, then re save. The issue I'm having is the save/open feature. How Do I add this? Or at least where can I start looking fro this solution?
If you are asking for an only-JS solution to this:
Opening Files
You can use the FileAPI to open the files from your users local machine, as long as its your users who are selecting the files manually(you are not digging into the file system yourself).
Here is nice demo for this functionality: http://html5demos.com/file-api
Saving Files
However, saving files was a W3C recommendation that got ditched so your only choice here is to allow your users to download the file. See this relevant question I made some time ago
If I were doing this for a class or prototype I might use PUT or POST requests to save the beat sequence in json form to a web server. When it's time to save your work, take your beat sequence data, convert from a js (I presume) object to a json string and PUT it to an appropriate url on your server. Give it a name and use the name in the url path, like "myserver.com/beats/jpbeat1.json".
When you want to retrieve that beat, issue an HTTP get to the url you saved. You can, of course also maintain and rewrite a directory page in json or html, or you can just navigate the directory offered by your web server. Most developers will think this crude, I imagine, but in a race for time, crude can be good if it meets the requirements.
Choose a web server, the easiest one for you. I might use Amazon S3. Costs money, but available to the internet, has authentication, security features, and supports http GET, PUT of static files. There are many other site hosting services.
For a local install, I might use Node.js. Also very easy to set up to serve static content. For example, the Express framework has static content service and easy examples how to set it up if you are comfortable with Javascript. With Node, you can make the site a bit fancier, by writing server side code to maintain an index, for example. Any real world implementation would require at least some server side code.
There are many other choices. Most developers would want a database, but you don't have to have one if simple files are ok for your class project. It all depends on your requirements.
I hope those ideas are helpful.

Categories