diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Dockerfile | 16 | ||||
| -rw-r--r-- | README.md | 69 | ||||
| -rw-r--r-- | app/app.js (renamed from app.js) | 3 | ||||
| -rw-r--r-- | app/benchmark/find.js (renamed from benchmark/find.js) | 9 | ||||
| -rw-r--r-- | app/benchmark/match-str.js (renamed from benchmark/match-str.js) | 8 | ||||
| -rw-r--r-- | app/pieces/prepare.js (renamed from pieces/prepare.js) | 4 | ||||
| -rw-r--r-- | app/pieces/retro-search.js (renamed from pieces/retro-search.js) | 3 | ||||
| -rw-r--r-- | app/pieces/suggest.js (renamed from pieces/suggest.js) | 0 | ||||
| -rw-r--r-- | app/routes/dev.js (renamed from routes/dev.js) | 0 | ||||
| -rw-r--r-- | app/routes/index.js (renamed from routes/index.js) | 0 | ||||
| -rw-r--r-- | app/routes/v1.js (renamed from routes/v1.js) | 3 | ||||
| -rw-r--r-- | app/utils/kb-util.js (renamed from utils/kb-util.js) | 0 | ||||
| -rw-r--r-- | app/utils/match-util.js (renamed from utils/match-util.js) | 0 | ||||
| -rw-r--r-- | app/utils/mem-usage.js (renamed from utils/mem-usage.js) | 0 | ||||
| -rw-r--r-- | app/utils/url-util.js (renamed from utils/url-util.js) | 0 | ||||
| -rw-r--r-- | data/.gitkeep | 0 | ||||
| -rw-r--r-- | data/products.null | 1 | ||||
| -rw-r--r-- | docker-compose.yml | 16 | ||||
| -rw-r--r-- | package.json | 2 |
20 files changed, 124 insertions, 12 deletions
@@ -1,3 +1,3 @@ node_modules/ -data/* +data/*.json .env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9cf8f84 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM node:alpine +WORKDIR /home/node + +# using wildcard (*) to copy both package.json and package-lock.json +COPY package*.json /home/node/ + +# create a null-data json-files +COPY data/products.null /home/node/data/products.json +COPY package.json /home/node/package.json +RUN npm i + +# create and set app directory as current dir +WORKDIR /home/node/app +COPY app/ /home/node/app/ +EXPOSE 3000 +CMD ["node", "app.js"]
\ No newline at end of file @@ -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); + @@ -17,4 +17,5 @@ let _r = require('./routes'); app.use(_r.routes()).use(_r.allowedMethods()); // start server listening on APP_PORT -app.listen(process.env.APP_PORT); +// app.listen(process.env.APP_PORT); +app.listen(3000); diff --git a/benchmark/find.js b/app/benchmark/find.js index 2d18657..1e86c24 100644 --- a/benchmark/find.js +++ b/app/benchmark/find.js @@ -5,7 +5,14 @@ const microtime = require('microtime'); -const products = require('../data/products.json'); +var products = require('../../data/products.json'); +if (products.length == 0) { + const prepare = require('../pieces/prepare.js'); + prepare.load_products( + 'https://storage.googleapis.com/pythia-files/uploads/json/emarket-products.json' + ); +} + var selected = []; diff --git a/benchmark/match-str.js b/app/benchmark/match-str.js index 2114f41..225cbc2 100644 --- a/benchmark/match-str.js +++ b/app/benchmark/match-str.js @@ -4,7 +4,13 @@ const match = require('../utils/match-util.js'); const memory_usage = require('../utils/mem-usage.js'); const kb = require('../utils/kb-util.js'); -const products = require('../data/products.json'); +// const products = require('../../data/products.json'); +var products; +try { + products = require('../../data/products.json'); +} catch (e) { + products = []; +} // memory_usage.report(); diff --git a/pieces/prepare.js b/app/pieces/prepare.js index 65ccee1..20825cd 100644 --- a/pieces/prepare.js +++ b/app/pieces/prepare.js @@ -42,7 +42,7 @@ function create(entity, url) { if (!error && response.statusCode == 200) { // body is a ready json-string; no need to parse and (re-)stringify try { - fs.writeFileSync(`${__dirname}/../data/${entity}.json`, body, 'utf-8'); + fs.writeFileSync(`${__dirname}/../../data/${entity}.json`, body, 'utf-8'); // file written successfully } catch (err) { console.error(err); @@ -88,7 +88,7 @@ function load_products(url) { try { fs.writeFileSync( - `${__dirname}/../data/products.json`, + `${__dirname}/../../data/products.json`, JSON.stringify(newJson), 'utf-8' ); diff --git a/pieces/retro-search.js b/app/pieces/retro-search.js index ba4d937..4e26720 100644 --- a/pieces/retro-search.js +++ b/app/pieces/retro-search.js @@ -1,7 +1,8 @@ const kb = require('../utils/kb-util.js'); const match = require('../utils/match-util.js'); -const products = require('../data/products.json'); +const products = require('../../data/products.json'); + /** VARIABLES * may passed as module arguments diff --git a/pieces/suggest.js b/app/pieces/suggest.js index c0a6e04..c0a6e04 100644 --- a/pieces/suggest.js +++ b/app/pieces/suggest.js diff --git a/routes/dev.js b/app/routes/dev.js index cd40822..cd40822 100644 --- a/routes/dev.js +++ b/app/routes/dev.js diff --git a/routes/index.js b/app/routes/index.js index e3d6230..e3d6230 100644 --- a/routes/index.js +++ b/app/routes/index.js diff --git a/routes/v1.js b/app/routes/v1.js index 9690973..394dde6 100644 --- a/routes/v1.js +++ b/app/routes/v1.js @@ -28,7 +28,8 @@ router.get('/prepare/:title', (ctx) => { break; } - return { success: result }; + console.log(result); + ctx.body = { success: result }; }) diff --git a/utils/kb-util.js b/app/utils/kb-util.js index aac88f9..aac88f9 100644 --- a/utils/kb-util.js +++ b/app/utils/kb-util.js diff --git a/utils/match-util.js b/app/utils/match-util.js index 5e8c648..5e8c648 100644 --- a/utils/match-util.js +++ b/app/utils/match-util.js diff --git a/utils/mem-usage.js b/app/utils/mem-usage.js index 766a93e..766a93e 100644 --- a/utils/mem-usage.js +++ b/app/utils/mem-usage.js diff --git a/utils/url-util.js b/app/utils/url-util.js index e86ad9b..e86ad9b 100644 --- a/utils/url-util.js +++ b/app/utils/url-util.js diff --git a/data/.gitkeep b/data/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/data/.gitkeep diff --git a/data/products.null b/data/products.null new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/data/products.null @@ -0,0 +1 @@ +[]
\ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..25c3319 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +services: + app: + image: node:alpine + user: "node" + working_dir: /home/node/app + environment: + - NODE_ENV=production + ports: + - "3000:3000" + volumes: + - ./app:/home/node/app + - ./data:/home/node/data + - ./node_modules:/home/node/node_modules + expose: + - "3000" + command: "node app.js"
\ No newline at end of file diff --git a/package.json b/package.json index 2ffe852..84a5010 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "oseine search engine is not elastic", "main": "index.js", "scripts": { - "start": "nodemon app.js", + "start": "nodemon ./app/app.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Geo-Xalkiadakis@Sklavenitis-SA", |
