Why is it necessary to restart a NodeJS server when there is a file change? Do other servers like Apache, IIS, nginx require this as well or can they restart automatically?
You don't say which files you're talking about so I'll mention the issues with a couple different types of files.
For Javascript files that make up your Javascript code for your node.js server, node.js is a continuously running server. That means when the server starts up, it parses your Javascript code into memory and then starts executing it. That server process stays running until you stop it. Because node.js is a continuously running server, if you want to update the Javascript files that make up the server code, you have to stop the server and restart it to let it load and reparse the newly changed source files.
This is very different than something like PHP with Apache that runs a given PHP script from scratch for each separate request. Since there is no long running PHP application and each PHP script is started from scratch for each request, then it can automatically pick up a newly changed PHP script without restarting the Apache server. If you had a long running server written entirely in PHP, then it would likely have similar behavior as node.js.
And, if you wanted to upgrade your Apache server code, you'd have to restart Apache (as with node.js).
You can kind of think of node.js as Apache + PHP in one since the functions of both are generally met by just node.js by itself. It integrates the web server functionality with the webapp logic whereas those are separate with Apache + PHP.
For HTML files or Javascript files that are served by the server and delivered to the browser, you will generally not have to restart the server for the new versions of those files to be served on subsequent browser requests. But, this depends a bit on which server framework you are using and how exactly it implements file caching. This behavior is not specific to node.js, but would be built into whatever code you were using to serve files by your web server (e.g. Express or something like that).
Related
I have an application built with Create React App that I want to use Node.js as a backend to load data from a database. Basically, I want Node to load the data and then use Create React App to do stuff with it. To do so I used this tutorial: https://www.freecodecamp.org/news/how-to-make-create-react-app-work-with-a-node-backend-api-7c5c48acb1b0/. I have set up a working development environment where I have all the Create React App in a client folder, a server.js that loads the data (from MS SQL Server if that's relevant), and a proxy set in client/package.json (CRA's package.json) for the port that the node app is running on (5000 in my case). I then run yarn dev and these two servers run together and talk to each other and everything works great.
The issue is deploying. My organization uses IIS. From what I can gather Node and IIS are not the ideal combination but IIS is what we use for everything. I would like the server.js and the CRA to be in the same folder and to basically function like one app as much as possible (I know that Node will need to run its own server). I would also like a setup that can be easily changed or moved to another machine. What I've done thus far is:
Set up a single application with a client folder, which is the build for CRA, and then server.js in the root
In my react code, made API calls to http://localhost:5000 (in the development environment I could do fetch('api/somestuff') but to make it work in production I needed to do fetch('http://localhost:5000/api/somestuff') )
Installed pm2 and used it to run server.js and start the Node server
This worked, however I'm wondering whether this is the optimal way to do things. This worked on my local machine but I don't know what will happen when I put it on our production machines. Will the Node server block other applications? Will it stay running no matter what or might it crash? Ultimately this is going to be deployed in a multi-server environment where we have 2 load-balanced servers with identical code that are put through an F5 to form a single URL--will this impact anything?
I'm pretty new to programming so bear with me, I'm sorry if this question was confusingly phrased.
I suggest you install ARR to work with IIS as a reverse proxy, which can forward the HTTP request to the backend NodeJS server.
Besides, for cross-domain request forwarding in the rewrite action type, we need to install Application Request Routing, and enable the proxy functionality.
Here are two examples of applying this feature, please check it.
How to successfully run node server with IIS?
ASP.net URL Rewrite subdirectory to external URL
Feel free to let me know if there is anything I can help with.
This might be a stupid question, but can I setup node.js to run an environment like WAMP? Just to test html docs, JavaScript, Json and Ajax requests.
If this is a good idea or possible, anyone have an advice on setting up the file structure?
You're misunderstanding some Node.js fundamentals. Let's break this down...
WAMP
W - Windows
You can install Node.js on Windows here. Nothing special.
A - Apache
While you can put Apache in front of node as a load balancer, for development purposes it's likely unnecessary. Node.js doesn't need Apache, or nginx. It will happily serve HTTP responses all on its lonesome.
M - MySQL
Sure, use MySQL if you want. :) You'll want to install MySQL on your machine, then you can get a driver for it on npm: https://www.npmjs.com/package/mysql
P - PHP
Node.js doesn't run PHP (barring a few most unholy abominations). Try taking a look at Jade.
The Basics
After you get Node.js installed, I'd recommend taking a look at Express and Jade. Start with the Express hello-world. You can run it simply by opening a command line, navigating to the project directory, and running node hello.js (or whatever you named the file). Just like that, you'll be serving HTTP requests. No need for a "stack".
I installed node.js on linux server. I'm able to run node.js on command line, but not able to run on browser.
Did I clearly explained what I want?
I have a domain ram.com that point to particular location on my server /var/www/html/ram.com/.
I created node.js pages on this location /var/www/html/ram.com .How can I access this page on browser?
My apache running on 80 port. Can you explain any changes in apache configuration?
I'm new to node.js can you explain clearly.
To use Node.js to serve your website, you just need to type node yourFileName.js in command line to start the server.
I don't really know how your pages look like. You need *.js files as Node.js source files, and Node.js work as backend. If you mean *.html, you can access them when the server program is running.
Actually, Node.js has its own built-in web server, just like PHP + Apache. So you don't need to use Apache, and I don't think Node.js and apache can work together without other tools.
This is my first answer in Stack Overflow, hope that can help you.
I have a nodejs app with modules, views etc..
Is it possible to open this app with node-webkit instead of opening it in the browser ?
Thanks.
Yes, it is. Node-WebKit is a package with browser + node web server. But I believe you will probably want to change your architecture, because you don't need the client-server style. If you run your app without any changes, node-WebKit will act just like node server and you still need a browser to access the app. There is no reason to use node-WebKit instead of pure node in this case.
To use node-WebKit embedded browser you should know that there are no need to start node server. The browser's JavaScript environment is already connected to node and you can execute node commands and packages direct from the JavaScript files (eg.: access file system from the browser, a dream to every web developer). It's like you are running a browser inside the server, without the need of make requests and receive responses... For this reason you don't need to use packages as socket.io, cause the communication is already established. But you can use the fact of node is a server to easily establish communication between different machines, for example.
I am coming from Microsoft world so please bear with me on this. I was told I could install node.js and use that as a web server instead of IIS. This is a very small business application. In IIS I can create virtual directory and point to the location of the web page and everything works just fine. Based on very little I read, I have few questions;
Is it possible to run node js as a windows service or any other form so that it runs for ever? I did find the forever package that I think I can use.
In IIS, I can create virtual directory set the port and thats it, I have myself a website.
I do not see any examples where I can use a directory where I have a web page, written in java script and point it to run as a web site. All the examples have some thing like server.js and that runs and routes the call. what is the other way to host web sites and use node.js to simple run as a fast web server.
I was told I could install node.js and use that as a web server instead of IIS.
This is true, but as you already found out then you are in charge of providing for things that IIS was already doing for you (e.g. automatically restart on reboot, or on crashes, hosting multiple sites by creating virtual folders, et cetera.)
You can indeed get all of these things worked out in Node.js and there are several libraries that help on each of these areas. It's not too hard but you'll need to do a bit of researching.
You can also run Node.js behind IIS. Take a look at iisnode http://tomasz.janczuk.org/2011/08/hosting-nodejs-applications-in-iis-on.html
Is it possible to run node js as a windows service or any other form so
that it runs for ever?
The library Forever takes care of restarting the site when it crashes...but I don't know if you can run it as a Windows Service. I haven't tried that.
In IIS, I can create virtual directory set the port and thats it,
I have myself a website.
I assume you are talking about a site that serves static HTML files, right? If that's the case that's very easy to support in Node.js either writing your own web server or using Express.js to serve static files.
I do not see any examples where I can use a directory where I have a web page,
written in java script and point it to run as a web site. All the examples
have some thing like server.js and that runs and routes the call.
Here is an extremely simple example to serve plain HTML files in Node.js https://gist.github.com/2573391 Don't use this in production, though. It's just an example and it does not have any kind of error handling or security.
what is the other way to host web sites and use node.js to simple run
as a fast web server.
As others have said, you should look into Express.js http://expressjs.com/ It provides some of the infrastructure that you are very likely going to need when building traditional web sites.
You say you're running a "very small business application" behind IIS. Unless it's written for Node.js (in JavaScript), it won't work.
There are no examples pointing to a directory and running that as a website, because that's not how things are done in Node.js. You write a Node.js-application and pull in a webserver-library.
Put simply, In Node.js, you don't embed the appliation in the webserver; you embed the webserver in the application.
When I used node.js, I redirected HTTP requests by a proxy server, nginx. I don’t know if you can directly bind node.js as an HTTP server, but for what’s it worth, nginx is pretty nice!
First things first, allow me to share an introduction. IMHO you should take this decision ( of moving from IIS to nodeJS) by adding various parameters. I belong to the Java & PHP community yet I use NodeJS to achieve extremely specific implementation where NodeJS perform the fastest ( fast IO, AJAX-JSON responses & more ). As you are coming with a Microsoft background you should bare with less comfortable solutions.
Yes, its possible to run NodeJs as a windows service and Forever will do fine.
and yes you can create "Virtual Directories" but by creating symbolic links to each of your customer's web site.
I recommend to take a good look at bouncy & express, If you're willing to take this step then these packages is just what you need.
Cheers!