Mean Stack Goodness

Wednesday, October 29, 2014

Download and install node.js

Download and install mongodb

npm install these modules into node_modules folder

To execute this JAVASCRIPT file in the node environment, execute node intro.js from command prompt

Let's create a module, in module folder create folder wordcop and add JAVASCRIPT file wordcoptext.js, this code verifies an input and changes words form the array to a sequence of hashes, the export command tells node which properties are to be exported, with require statement, JAVASCRIPT file gets loaded and these fields can be accessed from the module, basically the public api

The package.json file provides information about the module, the main property specifies the JAVASCRIPT executable to run, the engine specifies that this module will run with any version of node, repository is for online location where npm can pull the repository from

Create the package, navigate to the wordcop folder, execute npm pack command, this builds module and creates tgz file

Install wordcop package in readtext folder using npm install, it is now installed in nodemodules folder

Implement wordcop package with a require, bring in the package and assign it to a variable, then log some calls to the api, execute node readtext.js in the console

The function takes in a string and a callback function, callback event handler is implemented, first display log the element, then invoke process.nexttick, nexttick will queue up the request, whenever ready node event handler will pick it up and execute the callback function that was passed in and pass in the element, execute node in the command prompt, first the elements are logged and then the callback is processed, this is the node event callback model

Events and listeners part 1

Implementation and usage

Read and write to files, file io, load fs, nodes file system module, interact with files, write some data to the first file, hook into finish event, pipe method writes from one stream into the next, on readable and end events log data to the console, execute fileo.js in the command prompt, same principle applies to socket stream or httprequest

Start the mongodb database

Open another command prompt and start the mongo shell

Create new database using use command, this creates a handle, the new database isn't actually created untill collections and documents are added

To find documents in the collection use find command, none are returned since none created

Insert a document like this

Execute a find on the collection, view the document, an id for the object is automatically created

Insert multiple documents inside of an array

Do a find, view a total of four documents in the collection, just added three

Create a lunch collection and insert a document, insert another one and do a find to view the documents in the collection

Use findone to display first document found

Specify query object, only documents with result of thirteen

Query operators are prefixed with a dollar sign, this specifies a resultset of less than eleven

Use in to find from values in an array

Use and to find on multiple query operators in a single find operation

Assign cursor object to a variable, iterate over it and printjson the current document

The cursor variable holds all documents in the collection, call a count on it, get four

Limit limits to the number passed in, it grabs the first two documents

Find takes in as first parameter a query, in this case empty, second parameter the field to include or exclude, one means include, zero means exclude

In find clause, set id to zero, just retrieve the name field

Get distinct values for result field

Update document, change name, add a field and decrement a field

Update multiple documents, decrement score by 5, multi true applies to all documents

Find one object by name assign it to the doc variable, type doc to display it on screen, set result for doc object, type in doc view result has been updated, use save method on collection to persist to database, pass in object to be saved, view find with filter, result has been updated

Remove documents from collection using remove method

Remove all documents from collection, pass in an empty object into remove method

Drop a collection using drop method on collection

Drop database using dropdatabase method on database object

Default Success Warning Important Info Inverse
B S W I ? O