From d9364679a51ff80db8e5948ab089d749da36a6b2 Mon Sep 17 00:00:00 2001 From: Geo Halkiadakis Date: Wed, 17 Apr 2024 18:32:13 +0300 Subject: dockerize the app --- README.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 3 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 477d0cf..7f7c049 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +# Local installation + clone from ... git clone git@code.roptron.gr:/var/www/git/oseine.git @@ -19,7 +21,68 @@ run npm start -prepare source for production: -* remove ``/bench`` routes from ``paths.js`` -* remove ``/bench`` folder + +# Docker container preparation + +create the container: + + docker build -t oseine . + +build and start the instance for first time: + + docker-compose up --build + +start instance (any other time): + + docker-compose up + + + +# Notes + +## 1 Web Server for a node-js app: Older vs newer approaces + +Old .NET applications, Java (for applets, oldschool) and PHP are the odd ones out in that they need a webserver to run. + +It’s not the case for most others: Node, Rust, Go, pure Java applications, C++ and so on. + +I mean that in the sense that a dedicated webserver is not necessary, though a reverse proxy might be a good idea in some cases - but in the general case, no, you don’t need a web server for Node. + + +## 2 Choosing between Node-js and Apache server (pros+cons) + +The most important thing to understand is that one is "generally" not better than the other. The selection depends on the case in hand. + +* Apache generally uses PHP as a scripting language, which is extremely easy for the beginners and it is built for Web. Node js is simply a javascript v8 engine which was not originally intended to serve web. + +* Apache and PHP are old and stable. Everything you need to develop a working web application is available with a lot of support. Node Js is the new kid in the block ( .. not so new.. but still..). It is not essentially built for developing Web applications. Http based Web application can be considered as one use of Node Js. + +* Many famous cms (Wordpress, Drupal, Joomla etc) and development frameworks (Yii, Laravel, Code Ignitor, Cake PHP) are build in PHP. These frameworks are very helpful in organizing the code base for a large application. Node Js also has web frameworks like Express and sails js but they are new and the support is not as readily available as compared to PHP frameworks. + +* Apache is thread and process based i.e each request is handled by a separate thread or process (depending upon configuration), which means if the process is waiting for the I/O, whole thread is blocked. Node JS has asynchronous, event driven I/O. Every nodejs instance runs in a single thread and due to its asynchronous nature, it can handle far more number of concurrent requests as compared to apache. + +* Apache is thread based, so if an unexpected problem occurs while processing a request, only that particular thread will crash leaving rest of the requests and threads intact. Node Js handles multiple requests using one single thread. If a problem occurs whole node js instance will crash along with any global data that was stored in javascript variables or arrays. It can be automatically restarted using "forever" or some other modules but the current requests and the data is gone. + +* When Apache gets a request which is CPU intensive, other request do not get blocked because of the context switching between the threads. when NodeJs gets a CPU intensive request, all the other requests get blocked till this CPU intensive request stops for an I/O. its a good idea to delegate CPU intensive requests to a worker or some other process while using Node Js. + +* PHP does not support web sockets natively (to my knowledge). There are libraries which can help implement web sockets, but again due to thread based model, a decent number of live web socket connections will eat up your server's resources. NodeJs is a perfect candidate for real time communication over internet. Combine it with Socket.IO and you will get a good scalable web socket server along with fallback communication mediums such as flash socket and long polling. Its light weight and it can scale pretty good because its all running in one single thread. + + +## 3. Reloading a module on demand on node-js + +See [this!](https://stackoverflow.com/questions/33546880/node-js-how-to-reload-module). + + function nocache(module) { + require("fs").watchFile(require("path").resolve(module), () => { + delete require.cache[require.resolve(module)] + }) + } + +The function will delete your module from the cache each time the file changes. To use it, just paste it in the REPL, call nocache("d:/myapp.js"), then use require normally + + nocache('path/myapp.js); + var myapp = require('path/myapp.js'); + // ... + myapp = require('path/myapp.js); + -- cgit v1.2.3