Please pardon my ignorance on node.js. I have started reading on node.js and have some perception which might be wrong. So needed it to clarify
When we use createServer() method, does it creates a virtual server. Not sure whether the term "virtual" is appropriate, but it's the best I can describe it :)
I am confused that how should I deploy my application having node.js + other custom js files as a part of it. If I deploy my application in the main server, does that mean I have two servers?
Thanks for bearing with me.
I will try to answer that:
Q1:
createServer basically creates a process which listens on the specified port for the requests. So yes you can call it as a virtual server which constantly listens for request at the port.
Q2:
Yes you can say that it has now 2 servers
For eg: you server had apache initially which listens to port 80 (you can access it as http://example.com/ it by default looks for port 80)
and then you also start the node service listening on some other port for eg: port 8456 (you can access it as http://example.com:8456/ which will look for port 8456)
So yes you can there are two servers.
EDIT
Q: So what would be the difference if the page is served by the physical server and the virtual server created by node.js?
Physical Server and Node Server are 2 different things and there is no way a single request is going to both the servers.
For eg:
I use apache server to host my website running on PHP. It serves all the html contents of my website (which involves connecting to mysql for data).
Some of the requests could be:
http://example.com/reports.php
http://example.com/search.php
At the other end I might be using nodejs server for totally another purpose. For eg: I might use it for an API, which returns JSON/XML in return. I can use this API myself for some dynamic contents by making AJAX calls with javascript or simple CURL commands from PHP. Or I might also make this API available to public.
Some of the requests could be:
http://example.com:8456/getList?apikey=¶m1=¶m2=
My choice for NodeJs Server used as an API would be for its ability to handle concurrent request and since its asynchronous for file operations it will be much faster than PHP.
In this case I have a website which is not only working on PHP but its the combination of 2 different technologies (PHP on Apache and Nodejs) and hence 2 servers are totally different running on same server but have there own execution space.
Third Question:
So what would be the difference if the page is served by the physical server and the virtual server created by node.js?
If I might add, it's a virtual server in the sense that apache is an virtual http server listening on whatever port. Of course apache had a lot more modules and plugins and configurations to it where as Node's is lighter (kind of like WEBrick for rails), non-blocking and agile for building on. Then again apache is more stable.. in other words, it's a decision of software, both sitting on the server listening to a particular port set by you.
That said there's deployment methods that allow you to place a node application in front of software such as nginx (another server-side software) or HAproxy (load handling with a lot of power), so really it's all up to how you choose to configure it.
Maybe I'm getting to far from your question, but I hope this helps!
Also, You should give the answer to the other guy, he came first ;)
Related
sorry if this is a wrong question on this forum but I am simply just stuck and need some advice. I have a shared hosting service and a cloud based hosting server with node.js installed. I want to host my website as normal but I also want to add real time chat and location tracking using node.js I am confused with what I am reading in several places because node.js is itself a server but not designed to host websites? So I have to run 2 different servers? One for the website and one to run node.js? When I setup the cloud one with a node.js script running I can no longer access the webpages.
Whats the best way for me achieve this as I am just going round in circles. Also is there a way I can set up a server on my PC and run and test both of these together before hand so I see what is needed and get it working as it will stop me ordering servers I dont need.
Many thanks for any help or advice.
Node can serve webpages using a framework like Express, but can cause conflicts if run on the same port as another webserver program (Apache, etc). One solution could be to serve your webpages through your webserver on port 80 (or 443 for HTTPS) and run your node server on a different port in order to send information back and forth.
There are a number of ways you can achieve this but here is one popular approach.
You can use NGINX as your front facing web server and proxy the requests to your backend Node service.
In NGINX, for example, you will configure your upstream service as follows:
upstream lucyservice {
server 127.0.0.1:8000;
keepalive 64;
}
The 8000 you see above is just an example, you may be running your Node service on a different port.
Further in your config (in the server config section) you will proxy the requests to your service as follows:
location / {
proxy_pass http://lucyservice;
}
You're Node service can be running in a process manager like forever / pm2 etc. You can have multiple Node services running in a cluster depending on how many processors your machine has etc.
So to recap - your front facing web server will be handling all traffic on port 80 (HTTP) and or 443 (HTTPS) and this will proxy the requests to your Node service running on whatever port(s) you define. All of this can happen on one single server or multiple if you need / desire.
I have a website under domain (say example.com) which is hosted on Amazon Web Services EC 2 instance which has Apache already installed and ready to run on port 80.Now I wish to transfer from Apache to node JS (where Node JS runs on another port, say 8001).How to change the HTTP port address of EC 2 say that when i go to that URL (example.com) it should run on node JS instead of Apache (While for temporary,node JS runs on example.com:8001).
How is it possible and Kindly help?
So you cannot direct a standard web address (e.g. www.example.com) to anything other than port 80. By default http is on port 80 and https is on port 443. You can override that default by explicitly giving the port but you cannot change that default.
So your options are:
Replace Apache with Node on port 80. This will involve shutting down Apache (and making sure it doesn't auto restart on reboot), and changing your node port to port 80. This also will probably require running your node service as root (as port 80 is usually protected) and this is not recommended (Apache starts as root to get the port but then usually immediately switches to non-root user).
Have Apache proxy forward requests to Node. This means Apache is still your main webserver and listens on port 80 but certain requests are sent on to Node.
This second option could be done using mod_proxy with config like this:
ProxyPass "/foo" "http://localhost:8001/"
ProxyPassReverse "/foo" "http://localhost:8001/"
It all depends on what you want to use your set up for and making best use of the software available to you.
Typical set up is a multi-layered approach involving one or more of these:
LoadBalancer (optional for high load sites or where resiliency is key)
Webserver
Appserver
Database
Yes you could use just Node for all of these layers. However, to me, it is more an application server than a webserver.
A webserver like Apache or Nginx is specifically designed to act like a web server, and by that I mean serving static pages and doing other top layer stuff. They have several features built up over their years to provide speed and security. Now nearly everything they can do, can be done in Node but not quite as easily and not by standard and often requires pulling in 3rd party modules.
A webserver then typically offloads dynamic work to other programs. This could be scripts (PHP or Perl), or separate app servers like Tomcat, Jboss or Node. These are typically very good at specific tasks (e.g. talking to database and generating dynamic pages) but less good at severing static pages quickly.
The beauty of node to me is for micro- services, where you can have lots of independent, but potentially interlinked, node services which are all lightweight and good at one task and the web server is still needed in front of them. This compares to a bulky multi-tasking J2EE server like Tomcat or Jboss that you would use in the past which tried to do every dynamic app under one process (though admittedly often under separate WAR files).
So, without knowing your full use case, I would suggest Apache and Node instead of Node replacing Apache.
I never done any API, I just recently become aware of REST, never used sockets or node.js, but I have this simple project in mind using all of these.
Imagine usual app with request/response stuff. Nothing fancy. But then sometimes I need real time functionality, lets say there's a live support for website, a chat. So majority of users never need sockets and everything is easy, but when they do, what's then? How that would look and work with restful api?
As you tag, socket.io is perfect for you. It creates a socket within the browser to your server without the user installing any third party program, using websockets and longpolling. And for the users that have old browsers and don't have those browser built-in functions, it can fallback to a third party plugin: Flash Player, but almost all browsers have it installed.
Is you are used to Javascript or object oriented programming, socket.io and node.js is a walk in the park. If you don't want to use node.js and socket.io, you can write your own implementation of client-server with this info:
WebSockets
Long Polling example
Flash AS3 Socket
As a small adition, simply you need your default web server (Apache, Nginx, Lighthttpd, whatever...) running in default port 80 and also running a node.js server in other port, let's say 8080. That second server will serve all the files needed to connect, because socket.io can only connect to the same domain and port that served the files (security reasons, I guess).
In short, you'll have 2 servers: One serving your entire webpage and another one serving the files needed to connect to your chat (and also serving the chat, obviously).
I have exactly that configuration made in one of my pages (a live sports streaming site) and to add the chat to my site I have this server running in port 8080 and I load it in the main page inside an iframe: http://www.example.com:8080/
As an adition, you can create a complete http server in node.js, but I don't guess that it is useful as a professional web server.
For example, I have a sails.js application that (by default) listens on port 1337. I want to configure it to listen on two different ports at the same time - one for SSL and one for non-SSL traffic. Is this even possible? I have scoured the documentation and cannot find an example that shows me anything other than setting a single port value.
Do I have to create an front-end that (like Apache or nginx) to do it is it it possible to stick with a pure node.js solution - perhaps with express?
I should add that I am only using the server for web sockets via socket.io
A working example would be great, but any tips and pointers would help.
the simplest would probably be to run your server twice.
Just make sure you share common data like sessions and persistent global variables - maybe using something like redis (sails can automatically base your session on redis and can even bind a model on the redis server while keeping the rest on your current database)
I have a server running webmin (different domains pointing to different app/directories). Currently I can have my php app running from a directory and all I need to do in order to make it live is get webmin to direct that domain to that specific directory.
Can I do the same with a node js app? If not, how can I use node and webmin in the same box?
I know you didn't say this specifically, but assuming you're hosting the other web stuff through, say, Apache, you would need to leverage that, but you can probably get the effect you want. Basically, it sounds like you want to be able to use "host header" separation for services, rather than having a separate IP address for, say, Apache and Node.js to each use.
So, if you let Apache bind to the main port you're using (80/443/both), then you would run ode and have it configured to listen on a different port (say 8080 as in the example you left in another comment). You can then use mod_proxy in Apache and have it route request with certain domain names to Node. Here's a: more concrete example of this but really the idea is not specific to Node. It can apply to any other process that wants to respond to HTTP requests on your server (or even on a different server).