Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Logicify/nodejs-sample-task

Repository files navigation

nodejs-sample-task

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/

Content

Prerequisites

  • Mongodb (2.x)
  • Elasticsearch (last version)
  • Node v0.10 with npm

Details about installation of these can be found in Prerequisites section

Building and installing

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!

Test project

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.

Features list

We have a lot to show in our sample project. Below please find a list of features you may probably be interested in.

express.js

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 express
  • server.js file is used with npm start to bootstrap and run server

mongodb

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

elasticsearch

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.

extjs

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 and public/app.js file contain all the files with extjs code
  • public folder mapped with our Application as a placeholder of static files as well

json-validation

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 library
  • book/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

basic authentication

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 from app.js. Look for authInit and protectWithAuth functions
  • Explore related tests

multi environment configuration

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

https server

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

Jade template engine

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 in app.js
  • files /public/index.html and /public/secret.html deleted as they no more needed
  • app.js and package.json are changed to reflect our additions

Changelog

2014/04/04

  • taken Jade template engine in use, added 404 page

2014/03/28

  • added https server

2014/03/27

  • created multi environment configuration

2014/03/26

  • changes in startup procedure

2014/03/07

  • added json-validation feature
  • added basic auth feature

Team

Thanks to everyone who contributed to this sample application:

  • Pavel Knorr
  • Vadim Didenko
  • Igor Cherednichenko
  • Alex Cherednichenko
  • Konstantin Kovtushenko
  • Sergey Tityenok

Releases

No releases published

Packages

No packages published