I have over 20 years experience in PHP programming, started and tried to learn Node Js in almost half year, but still not yet understanding how to use it...
I saw many samples on learning Node Js with ExpressJs, which is a framework can be served a website/app on a port (e.g. 3000). But, would there a way working without ExpressJs? I tried to upload that files directly to server, but it turned out loading index.html, not index.js
I also tried to call it as Module in my existing html file, but while calling 'import' it fire an error
Relative module specifiers must start with “./”, “../” or “/”.
Since I don't want to separate main site (:80) and node js (:3000) to different sites, how can I get it work together? Do I need to rewrite whole the existing website (in PHP) to node js and map :3000 to :80 for that?
Is it a general way most node js app running?
Thanks so much.
I'll summarise PHP first as a point of reference since you are familiar with it already.
To run a server-side PHP program you typically pick an HTTP server, such as Apache HTTP. A default configuration of this will map URLs onto a path on the file system. For example, asking the server for /foo.html will load the file stored at /var/www/html/foo.html.
You configure the server so if the file ends in .php then instead of sending the file directly to the browser, it is executed with PHP and the output is sent to the browser.
If you want two URLs to trigger PHP code, then you put a .php file at both.
That's a simple approach to running PHP. More complex options are available (with pros and cons).
There are lots of hosting plans designed to support PHP programs running this way. So many that if you buy a cheap hosting plan then you can often just assume it will support this approach.
Node.js is a program for executing JavaScript.
A typical approach to using it for server-side programming involves writing a JavaScript program that creates a webserver that you use instead of something like Apache HTTPD.
Normally you would write that webserver using something like Express.js (which takes all the pain out of it).
This JavaScript program is responsible for handling the URLs (since you aren't using Apache, it won't do it for you). This is why Express.js is popular: It makes mapping URLs onto functions easy.
To set up Node.js on your hosting plan, you need either:
You generally do not find hosting plans that are "drop some JS files onto a server and it will just work".
Hosting services like Heroku, designed for running Node.js projects, will have straight forward setup instructions.
If you are doing everything yourself, then a typical set up process would look something like:
npm ipm2 that will restart it automatically if it falls over or the server rebootsOften you will use something like Docker to bundle everything up in a package that you can just deploy to a service that supports Docker containers.
You can also take a significantly different approach and use something like a AWS lambda function.
Since I don't want to separate main site (:80) and node js (:3000) to different sites, how can I get it work together?
You can run a Node.js server on one port, and Apache (or another HTTPD on another) and proxy requests from Apache to Node.
Even if you aren't using PHP, this is a useful approach to have a more efficient service for handling static files (e.g. images and css) and lets you add caching features between the server-side code and the client.