This is a sample application, providing a bookstorage-like functionality over the web, in order to showcase and expose technical stack and techniquest to be used on larger projects.
Sample task for nodejs, extjs, mongodb, elasticSearch. The app is ready to be deployed to the Heroku cloud hosting, however may also be easily ran locally.
Created and maintained by Logicify http://logicify.com. We use it also as a kickstarter app when we need to have a typical one started. It is released under MIT, and really is not a rocket science - please feel free to fork it and use. Pull requests with improvements and new features are also welcome.
You can see the app instance at http://http://nodejs-sample-task.logicify.com/
- Mongodb (2.x)
- Elasticsearch (last version)
- Node v0.10 with npm
Details about installation of these can be found in Prerequisites section
Just make sure mongodb and ellasticsearch services are up and running.
Then, carry on to building application. First, run
npm install
This will install the dependencies. Then,
npm start
This will launch our init script. You will be prompted to insert demo data into the DB. We strongly recommend to do it before the first time launch. On the command line where you ran the node, you will observe the notice with the URL the app is accessible on. Visit that URL in browser, and you are in!
First, make sure the project is built and running successfully.
Then simply run the
npm test
from within the project directory. This should result in the tests running and producing a specification-like output of the tests.
We have a lot to show in our sample project. Below please find a list of features you may probably be interested in.
All this application is based on express.js library. This library suits our aim fine and it is easy to setup. So not much to talk, just check out our code.
Related code parts:
app.js
file contains our Application object. This object is build on top of expressserver.js
file is used withnpm start
to bootstrap and run server
It is very powerful document database. You can read about Mongo by link in more detail. We use Mongo to store our book objects as a json-based documents.
Related code parts:
book-data-provider.js
encapsulates all methods related to work with mongo
This feature is used for fast full text search through all books stored in the system. Here is the related link to elasticsearch site
Code parts:
book-search.js
file with Search object. This object has methods related to work with elasticsearch.
Basic UI of our application is built up using Sencha Ext JS
project. Check this link to learn more about this library.
Main concept of using extjs is to build one page rich web application.
Related code parts:
public/app
folder andpublic/app.js
file contain all the files with extjs codepublic
folder mapped with ourApplication
as a placeholder of static files as well
We obviously need to validate user's input before processing. On large number of input it can become a mess. Its great to have strict data contract between input and processing backend. Good way to achieve this is the usage of JSON schema based validator.
Please checkout json-validator and related json-schema notation.
Related code parts:
- json-schema dependency in
package.json
lib/validator.js
librarybook/book-validation-schema.js
file with all book validation schemas- mountAPI method of the Application object in the
app.js
file. Here we add validation middleware to every routes required for user data checks
As an example of authentication and authorization process usage we've selected the basic authentication approach. In our nodejs-sample-task we check for a single user's credentials with name: "valid" and password: "user".
Related code parts:
Application
fromapp.js
. Look for authInit and protectWithAuth functions- Explore related tests
It seems that any project should have different configurations to run at least in three configuration environments, such are:
- test
- development
- production
Those configurations are created in our sample task and code related to those changes has been changed accordingly.
Related code parts:
- created
configuration
folder with 4 js files configuration/index.js
- a single entry point for any configuration
Every web application should receive and send personal user information in most situations. And our application does not stand alone.
User sends their credentials, book content can be secure and so on. That is why we have implemented new feature - https server.
Please look at this heroku article how to aquire ssl sertificate. With our app we were going through detailed instructions of this article.
Related code parts:
/certificates/https/server.key
- private key/certificates/https/server.csr
- certificate signing request/certificates/https/server.crt
- self signed ssl certificate- made new records in the configuration files
- added secure server in
Application
object, have made auto redirection to secure entry point - added new record to start script
server.js
Every web application has couple of static pages, which are - index page, 404 page, login/logout pages etc.
These pages have many common parts. And there is a very good point to use template engine to encapsulate this commons.
Related code parts:
- created
/view
directory with all*.jade
files /routes/index.js
file with our static routes mounted inapp.js
- files
/public/index.html
and/public/secret.html
deleted as they no more needed app.js
andpackage.json
are changed to reflect our additions
- taken Jade template engine in use, added 404 page
- added https server
- created multi environment configuration
- changes in startup procedure
- added json-validation feature
- added basic auth feature
Thanks to everyone who contributed to this sample application:
- Pavel Knorr
- Vadim Didenko
- Igor Cherednichenko
- Alex Cherednichenko
- Konstantin Kovtushenko
- Sergey Tityenok