Events in Node

Events lie at the heart of Node.js. In fact, events ARE the heart of Node.js. When building our own custom modules, we are able to make use of this functionality provided by Node.js for emitting our very own events. We can do this by using the EventEmitter exposed by the built-in ‘events’ module. The following code snippet demonstrates how to use the simple API of the EventEmitter.

var events = require(‘events’);
var eventEmitter = new events.EventEmitter();
eventEmitter.on(‘someOccurence’, function(message){
console.log(message);
});
eventEmitter.emit(‘someOccurence’, ‘Something happened!’);

After creating an eventEmitter object, we subscribe to an event named ‘someOccurence’ and register a function with a message argument to be called when the event is raised. Subscribing is done by executing the on() method of the eventEmitter object. Next we raise our event by simply calling the emit() method of the eventEmitter object, also passing in the value for the message argument of the event handler function.


ProsperaSoft offers Node.js development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Node.js experts and consultants.

 

How to create HTTP server in Node?

To create HTTP server in node, we have to include http module. As shown below:

var http = require(“http”);

// request variable holds all request parameters
// response variable allows you to do anything with response sent to the client.
http.createServer(function (request, response) {
// Attach listener on end event.
// This event is called when client sent all data and is waiting for response.
request.on(“end”, function () {
// Write headers to the response.
// 200 is HTTP status code (this one means success)
// Second parameter holds header fields in object
// We are sending plain text, so Content-Type should be text/plain
response.writeHead(200, {
‘Content-Type': ‘text/plain’
});
// Send data and end response.
response.end(‘Hello HTTP!’);
});
// Listen on the 8080 port.
}).listen(8080);

This code is very simple. You can send more data to the client by using theresponse.write() method, but you have to call it before calling response.end().

Now, save file as server.js and in your console write following:

$node server.js

Open up your browser and navigate to http://localhost:8080. You should see the text “Hello HTTP!” in the page.


ProsperaSoft offers Node.js development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Node.js experts and consultants.

 

File Uploads in Node.js

We will use Express Framework  and middleware called “multer”.  This middleware is designed for handling the multipart/form-data.

To install multer write following command:

$ npm install multer

To configure multer add following code in server.js:

app.use(multer({ dest: ‘./uploads/’,
rename: function (fieldname, filename) {
return filename+Date.now();
},
onFileUploadStart: function (file) {
console.log(file.originalname + ‘ is starting …’)
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ‘ uploaded to ‘ + file.path)
done=true;
}
}));

Multer emits event on particular situation such as we used onFileUploadStart which means when file start uploading, this event will be emitted.

As soon as onFileUploadComplete event emitted we have set the variable done to true and use it in our router to determine whether file is uploaded or not.

For handling routes, add following code in server.js:

app.get(‘/’,function(req,res){
res.sendfile(“index.html”);
});

app.post(‘/api/photo’,function(req,res){
if(done==true){
console.log(req.files);
res.end(“File uploaded.”);
}
});

To run server add following in server.js:

app.listen(3000,function(){
console.log(“Working on port 3000″);
});

Now, in your HTML file add following:

<form id = “uploadForm”
enctype = “multipart/form-data”
action = “/api/photo”
method = “post”
>
<input type=”file” name=”userPhoto” />
<input type=”submit” value=”Upload Image” name=”submit”>
</form>

In HTML form you must mention enctype=”multipart/form-data” else multer will not work.

Now run your project :

$ node server.js

And visit localhost:3000 to view running application.

To perform validation on Server end, multer provides limits array parameter. If you want you can perform validation as shown below:

app.use(multer({ dest: ‘./uploads/’,
rename: function (fieldname, filename) {
return filename+Date.now();
},
limits: {
fieldNameSize: 100,
files: 2,
fields: 5
}
}));

Multer provides easy way to upload files in Node.js.


ProsperaSoft offers Node.js development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Node.js experts and consultants.

 

How to send e-mail in Node.js?

In most of real time application we need to send mails for account verification, account recovery, etc. Hence, for Node applications also we need to find some way to send email.

For that Node already have nodemailer module which makes task very easy.

First of all, you will need to install nodemailer and express.js.

npm install nodemailer

npm install express

Then setup your server.js file.

var express=require(‘express’);
var nodemailer = require(“nodemailer”);
var app=express();
app.listen(3000,function(){
console.log(“Express Started on Port 3000″);
});

In your HTML file, you will need add code to hit server.js to send mail. Hence, add following code to your HTML file.

$(document).ready(function(){
var from,to,subject,text;
$(“#send_email”).click(function(){  // send_email is id of button 
to=$(“#to”).val();
subject=$(“#subject”).val();
text=$(“#content”).val();
$(“#message”).text(“Sending E-mail…Please wait”);
$.get(“http://localhost:3000/send”,{to:to,subject:subject,text:text},function(data){
if(data==”sent”)
{
$(“#message”).empty().html(“Email is been sent at “+to+” . Please check inbox !”);

}});

});

});

As we are making request to server we need to handle this in server side also.

app.get(‘/send’,function(req,res){
//code to send e-mail.
});

Now, add nodemailer code,

var smtpTransport = nodemailer.createTransport(“SMTP”,{
service: “Gmail”,
auth: {
user: “yourID@gmail.com”,
pass: “Your Gmail Password”
}
});

Add above code in server.js ,

var smtpTransport = nodemailer.createTransport(“SMTP”,{
service: “Gmail”,
auth: {
user: “yourID@gmail.com”,
pass: “Your G-mail password”
}
});

app.get(‘/’,function(req,res){
res.sendfile(‘index.html’);
});
app.get(‘/send’,function(req,res){
var mailOptions={
to : req.query.to,
subject : req.query.subject,
text : req.query.text
}
console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
res.end(“error”);
}else{
console.log(“Message sent: “ + response.message);
res.end(“sent”);
}
});
});


ProsperaSoft offers Node.js development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Node.js experts and consultants.

 

 

How to do String validation in Node.js?

In all real time application we need to use validation so that proper data is been entered by user. Lets see how we can do it in Node.js:

Node.js provides ‘validator‘ library to do validation. To install validator do following:

npm install validator

This package covers most of the string sanitization and validation.

For example, isEmail(), isURL(), isIP(), etc.

On server side you need to check validation as shown below:

var validator = require(validator);
validator.isEmail(mail_id _from_params); 
On client side do following:
Add <script type=”text/javascript” src=”validator.min.js”></script>
validator.isEmail(foo@bar.com); // return true

ProsperaSoft offers Node.js development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Node.js experts and consultants.

 

Solr query syntax

The main query for a solr search is specified via the q parameter.  Example of standard query is shown below:

http://localhost:8983/solr/query?q=test

If you add debug=query, you can see see how Solr is parsing your query. You can do as shown below:

http://localhost:8983/solr/query?debug=query&q=hello

Example of solr query is shown below:

{
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "q":"hello",
      "debug":"query"}},
  "response":{"numFound":0,"start":0,"docs":[]
  },
  "debug":{
    "rawquerystring":"hello",
    "querystring":"hello",
    "parsedquery":"text:hello",
    "parsedquery_toString":"text:hello",
    "QParser":"LuceneQParser"}}
The response section will normally contain the top ranking documents for the query. In the above example, no document matched query.
In the debug section, one can see how the query was parsed, and the fact that text was used as the default field to search.
Basic Queries:
A “term” query is a single word query in a single field that must match exactly. For example,
text:hello
Here, ‘text’ is field name, and ‘hello' is the word we are going to match.
Phrase Query:
A phrase query matches multiple terms (words) in sequence.
text:”john smith”
This query will match text containing Yonik Seeley but will not match smith  v john or smith john .
Proximity Query:
A proximity query, is like a phrase query with a tilda (~) followed by a slop that specifies the number of term position moves (edits) allowed.text:”solr analytics”~1
This query will match text containing solr analytics, solr faceted analytics (edit distance 1), and analytics solr (edit distance 1). It will not match solr super faceted analytics or analytics faceted solr since those would both require an edit distance of 2 to get the terms into the matching positions.

Boolean Query:
A boolean query contains multiple clauses. A clause may be optional, mandatory, or prohibited.solr search
The default operator is “OR”, meaning that clauses are optional. When there are no mandatory clauses, at least one of the optional clauses in a query must match for the full query to match. The example query above will thus match documents containing solr or search (or both) in the default search field.

Boolean query examples:

+solr +search facet -highlight /* uses + for mandatory and – for prohibited */
solr AND search OR facet NOT highlight /* this is equivalent to the first query */
Semantics: solr and search must both match, highlight must not match. facet may or may not match but will contribute to the query score if it does (i.e. the presence of the facet only affects scores of matching documents, not which documents match.)

Boosted Query:
Any query clause can be boosted with the ^ operator. The boost is multiplied into the normal score for the clause and will affect its importance relative to other clauses.Boosted Query Examples:

text:solr^10 text:rocks
(inStock:true AND text:solr)^123.45 text:hi

Range Query:
A range query selects documents with values between a specified lower and upper bound. Range queries work on numeric fields, date fields, and even string and text fields.Range Query Examples:

age:[18 TO 30] // matches age 18-30 inclusive (endpoints 18 and 30 are included)
age:[65 TO *]        // “open ended” range matches age>65

Constant Score Query:
A ConstantScoreQuery is like a boosted query, but it produces the same score for every document that matches the query. The score produced is equal to the query boost. The ^= operator is used to turn any query clause into a ConstantScoreQuery.Constant Score Query Examples:

+color:blue^=1 text:shoes
(inStock:true text:solr)^=100 native code faceting

Filter Query:
A filter query retrieves a set of documents matching a query from the filter cache. Since scores are not cached, all documents that match the filter produce the same score (0 by default). Cached filters will be extremely fast when they are used again in another query.Filter Query Example:

description:HDTV OR filter(+promotion:tv +promotion_date:[NOW/DAY-7DAYS TO NOW/DAY+1DAY])

Query Comments:
One can embed comments in a query using C-style comments surrounded with /* */. Comments can be nested.Query Comments Example:
description:HDTV /* TODO: +promotion:tv +promotion_date:[NOW/DAY-7DAYS TO NOW/DAY+1DAY] */


ProsperaSoft offers Solr development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Solr experts and consultants.

 

How to make search request on Solr?

Solr is able to achieve fast search responses because, instead of searching the text directly, it searches an index instead.

You can make search request on Solr in following ways:

Search Request Query example:

Suppose, we need to get result of all books with title ‘Java’.

Then we would need to write query as shown below:

 http://localhost:8983/solr/demo/query?
q=title_t:java
fl=author_s,title_t

Here, in above example ‘fl’ is used specify which fields should be returned from documents matching the query.

We should see a result like the following:

{“response”:{“numFound”:2,”start”:0,”docs”:[
{
"title_t":"The Java Book",
"author_s":"Abc"},
{
"title_t":"Java Black Ook",
"author_s":"Def"}]
}}

Solr Search Request in JSON:

If you prefer using JSON to search the index, you can use the JSON Request API:

$ curl http://localhost:8983/solr/demo/query -d '
{
  "query" : "title_t:java",
  "fields" : ["title_t", "author_s"]
}'
Sorting and Paging Search Results:
By default, Solr  will return the top 10 documents ordered by highest score (relevance) first. We can change this count as shown below:
$ curl http://localhost:8983/solr/demo/query -d ‘
q=*:*&
fq=publisher_s:Abc&  // filter query based on publisher
rows=3&
sort=pubyear_i desc& //sorts the “pubyear_i” field in descending
fl=title_t,pubyear_i’
To manage search result count in above query we use row=3.
And we get the response as requested:“response”:{“numFound”:5,”start”:0,”docs”:[
{
“pubyear_i”:1999,
“title_t”:["Abc"]},
{
“pubyear_i”:1996,
“title_t”:["Def"]},
{
“pubyear_i”:1992,
“title_t”:["Fgh"]}]
}


ProsperaSoft offers Solr development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Solr  experts and consultants.

 

 

Faceted search in Solr

Faceting is the arrangement of search results into categories based on indexed terms.  Faceted search provides an effective way to allow users to refine search results, continually drilling down until the desired items are found.

Implementing Faceting with Solr:

It’s  simple to get faceting information from Solr, as there are few prerequisites. Solr offers the following types of faceting:

Field faceting – retrieve the counts for all terms, or just the top terms in any given field. The field must be indexed.
Query faceting – return the number of documents in the current search results that also match the given query.

Faceting commands are added to any normal Solr query request, and the faceting counts come back in the same query response.

Example of Field Facet:

Suppose, user entered following query in search bar.The Solr query to retrieve the top “camera” matches would be:

http://localhost:8983/solr/query?q=camera

And now we want to add search on manufacturers also. we will just need to add following:

&facet=true
&facet.field=manu  // “manu” field available in schema

The query response will now contain facet count information for the given fields in addition to the top matches for the query.

“facet_fields” : {
“manu” : [
"Canon USA" , 25,
"Olympus" , 21,
"Sony" , 12,
"Panasonic" , 9,
"Nikon" , 4 ],

}

Example of Query facet:

For query facet , we will simply write facet.query command to our query request.

Here, we want to fetch result whose price lies between $100 or less, $100-$200.

&facet=true
&facet.query=price:[* TO 100]
&facet.query=price:[100 TO 200]

Response would be as shown below:

“facet_queries” : {
“price:[* TO 100]” : 28,
“price:[100 TO 200]” : 54,
}

Now let’s assume that the user wants to drill down on the constraint $400-$500 from the Price facet to get a new set of results that include only cameras in that price range. For this we use the fq (filter query) parameter, which allows one to filter by a query. We’ll also send the relevant faceting commands again since we also want to update the facet counts.

http://localhost:8983/solr/query?q=camera&facet=on&facet.field=manu&facet.field=camera_type&fq=price:[400 to 500]

There are so many facet.* available that you can use in your search query.


ProsperaSoft offers Solr development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Solr experts and consultants.

 

 

 

 

 

 

 

 

 

 

 

Analyzers, Tokenizers, and Filters in Solr

Understanding Analyzers, Tokenizers, and Filters in Solr:

Field analyzers are used both during ingestion, when a document is indexed, and at query time. An analyzer examines the text of fields and generates a token stream.

Tokenizers break field data into lexical units, or tokens.

Filters examine a stream of tokens and keep them, transform or discard them, or create new ones.

Analyzers:

An analyzer examines the text of fields and generates a token stream. Analyzers are specified as a child of the <fieldType> element in the schema.xml configuration file. For example:

<fieldType name=”nametext” class=”solr.TextField”>
<analyzer class=”org.apache.lucene.analysis.WhitespaceAnalyzer”/>
</fieldType>

In this case a single class, WhitespaceAnalyzer, is responsible for analyzing the content of the named text field and emitting the corresponding tokens.

Tokenizers:

The job of a tokenizer is to break up a stream of text into tokens, where each token is (usually) a sub-sequence of the characters in the text. An analyzer is aware of the field it is configured for, but a tokenizer is not. Tokenizers read from a character stream (a Reader) and produce a sequence of Token objects (a TokenStream). For example:

<fieldType name=”text” class=”solr.TextField”><analyzer>
<tokenizer class=”solr.StandardTokenizerFactory”/></analyzer>
</fieldType>

The class named in the tokenizer element is not the actual tokenizer, but rather a class that implements the org.apache.solr.analysis. TokenizerFactory interface. This factory class will be called upon to create new tokenizer instances as needed. Objects created by the factory must derive from org.apache.lucene.analysis.TokenStream, which indicates that they produce sequences of tokens. If the tokenizer produces tokens that are usable as is, it may be the only component of the analyzer. Otherwise, the tokenizer’s output tokens will serve as input to the first filter stage in the pipeline.

Filters:

Filters consume input and produce a stream of tokens.The job of a filter is usually easier than that of a tokenizer since in most cases a filter looks at each token in the stream sequentially and decides whether to pass it along, replace it or discard it. For example,

<fieldType name=”text” class=”solr.TextField”>
<analyzer>
<tokenizer class=”solr.StandardTokenizerFactory”/>
<filter class=”solr.StandardFilterFactory”/>
<filter class=”solr.LowerCaseFilterFactory”/>
<filter class=”solr.EnglishPorterFilterFactory”/>
</analyzer>
</fieldType>

This example starts with Solr’s standard tokenizer, which breaks the field’s text into tokens. Those tokens then pass through Solr’s standard filter, which removes dots from acronyms, and performs a few other common operations. All the tokens are then set to lowercase, which will facilitate case-insensitive matching at query time.


ProsperaSoft offers Solr development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Solr experts and consultants.

 

Solr Spellcheck

If Solr SpellCheck not working properly then do following:

Before indexing,

Remove/comment Stopfilterfactory from both index and query in analyzer.

Don’t use KeywordTokenizerFactory because it takes terms as a whole string. Use StandardTokenizerFactory.


ProsperaSoft offers Solr development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Solr experts and consultants.