📜 ⬆️ ⬇️

Impress: multipurpose application server for Node.js

Despite notable successes, Node.js is still a specialized technology, which mostly closes bottlenecks in systems written in a different technology stack. The reason for this situation lies in the fact that the node itself does not have many libraries that we are used to in other languages ​​and which provide rapid development of application software. For example, in order to separate handlers for different URLs in the code, give static files, organize sessions, run multiple threads, have access to the database, cache data in memory, differentiate user rights, have logs and rotate them, create a network API, render templates , configure URL-rewriting, provide fast delivery of events from the server to clients, for all of this, and many other tasks, separate libraries (modules) are used. Different modules are written by different developers, difficult to fit, conflict. In general, we decided to combine all this set of required functionality in virtually every web application into one application server and thus increase code connectivity, make the application server core monolithic and more consistent than solutions compiled from separate libraries . The Impress project has already been announced as a prototype , and now it provides the entire necessary arsenal for rapid application development, which has been tested on a dozen live projects. Impress differs significantly from another widely used platform, just as impressionism differs from expressionism, that is, it produces a holistic, well-thought-out aesthetic impression, as opposed to a sudden outburst of emotions. But we, without being involved in the criticism of someone else's code, proceed to the demonstration of the design features of Impress.

Some code metrics



Capabilities and applications



Well, and those opportunities that I will not detail, because Already wrote about them and their better look on examples:

Add-on MySQL access driver:

A metalanguage based on JSON syntax has also been developed, which allows us to conveniently and briefly describe the structures of relational databases and then translate these structures into SQL scripts. See examples in the / node_modules / impress / schemas / directory. For translation you can use the following code:
var schemaCore = require('./schemas/impress.core.schema.js'); var scriptCore = db.schema.mysql.generateScript(schemaCore, true).script; console.log(scriptCore); 

Further, schemes can be used for scaffolding of forms, grids, and user interfaces in general, but this is a topic for a separate publication.

Examples


It is best understood with examples that are enough in Impress. Immediately after installation from the NPM repository ( npm install impress ), we can deploy the examples by copying them from the / node_modules / impress / examples / copyContentToProjectFolder folder to the project root. For launch, MongoDB is desirable for storing sessions (other session storage providers will be implemented soon). But you can run the examples without the database. If MongoDB is still there, then you need to set “session.persist” to true in config.js, uncomment “databases.impress”, in the same config, and uncomment modules in the “plugins.require” section: “db”, “ db.mongodb "," impress.security.mongodb ". After that, create the necessary collections and indexes by running: node setup.js and then start the application server: node server.js

API (RPC): STATEful and STATEless


One of the main tasks for which Impress was developed is the creation of application servers both on the STATEless principle (ie, REST servers) and on the more interesting STATEful principle. It is necessary to remind that REST, is when between the request / response pair neither on the server nor on the client, the state of the object is not saved. In contrast to the RPC, on which client-server applications are based, in which it is customary to create a model in the client and create a model in the server, linking their interfaces over the network and translating events and calls between these models. Here the node allows you to deploy a model at two ends of the wire and synchronize calls via AJAX / JSON, which of course is more convenient for application applications. REST came to the node from the Stone Age heavyweight web servers (like Apache and IIS), which each time launched external (in relation to them) applications, sending them HTTP protocol requests through CGI. Such an application generates a new process, it must initialize the working environment, i.e. establish connections with the database, deploy all your data, read something from the file system (if needed), etc. and all this only in order to complete the work in a few mil milliseconds and free up memory, disconnect from the database. Before the web, I wrote in languages ​​that adopted the STATEful API as RPC (COM, DCOM, Corba ...), and for me the concept of REST was always lacking. And finally, after the transition to the religion of the node, I was happy. Now again you can expand the data in memory and they do not disappear from the request to the request, you can store weighty sessions in RAM and do not serialize / deserialize them at the end and restart processes. And I had a vision that REST is a thing of the past with viewstate type crutches and state servers. I understood that the STATEful API is the greatest blessing granted by the Almighty to every living being who has cognized a node.
')
To make a new API-URL handler, you just need:
1. Create the folder /api/myAPI/getSomething.json/
2. Put the post.js file there and write in it:
 module.exports = function(req, res, callback) { db.impress.collectionName.find({ fieldName: req.post.fieldValue }).toArray(function(err, nodes) { res.context.data = nodes; callback(); }); } 

3. In the / api / myAPI directory, create the access.js file and write in it:
 module.exports = { guests: false, logged: true, http: true, https: true } 

Everything is ready, for https just set up in config.js and disable access.js for http for this folder. Moreover, you can create handlers to restart the server, simply create another folder and write the code in the file there. When you first access the code gets into memory, when you change the file on the screw, the code loads a new one into memory and sits there and waits for a call.

Screens from the demo application


After installing and deploying the examples, you can see these screens. The first one is a user registration form, it works when MongoDB is connected, as well as all the functionality associated with accounts and stored sessions (Create account, Sign In, Sign out).


In the left column of the buttons that run the examples, they are best viewed with Firebug enabled or another browser developer tool.


The biggest example is the universal admin panel for MySQL and MongoDB , which I already wrote about. Here is its screenshot:


On Github: https://github.com/tshemsedinov/impress
In npm: https://npmjs.org/package/impress

Source: https://habr.com/ru/post/194250/


All Articles