As a developer, I always want my code to talk back to me and tell its story when I visit it after some time. This is what drove me to this blogging concept where I share how we can code/program so that it makes one's life easy as a developer.
We recently had a requirement where were required to update an array inside the document. Array is a filed inside my document apart from few simple datatypes.
Here is the current document structure
I want to add a JSON to this ARRAY field as shown below.
Once added, the document looks like this
This is how we do it in the code. There are many ways apart from this yet, I found this more readable and more easy to use.
Hi friends,
Here is the 3rd video in the series for learning Node.js and building a REST API. In this video, i give you a brief introduction to package.json file.
package.json file is the place where it holds few important information about our nodejs application. It holds the information like the dependencies, the dev-dependencies etc. So, if you install express, then the version of the express that you installed is mentioned here as a dependency.
Watch this 11 min video. As promised, i will make sure that i dont cross 15 min of time in video length. So that each video can separately answer you a concept.
Step by step installation of Nodejs application to Heroku Cloud platform for beginner
Hi friend, this time I have decided to write on how to deploy Nodejs application to Heroku cloud platform. This is such an important article on my blog and in my faith of sharing knowledge helps us more. This is because I have had a very tough time understanding on how Heroku works and how to push an application(Nodejs) in my case. I spent almost 2-3 days of sleepless nights apart from my working hours at office, and 3 hours of travel everyday.
So here we go...
Points to note, I did not address on how to create an account in Heroku or what is Heroku dashboard etc as they are basic steps and are easy to do. I have used Mongodb as my backend and I use a separate Mongo lab account and call it in my app than getting it registered inside the Heroku as an add on. The add on via Heroku is a chargeable account.
Visual Studio from Microsoft is mostly used as a paid one except for small application and demos that are done by people like us. For quick code snippets and tutorials, Visual Studio Express has been released a along with VS2010. From then on, Microsoft has been having an eye on the trends and developments in the market.
They have released plugins via their NuGet package manager for Node.js, CofeeScript and many others in the last few months.
After Satya Nadella became its CEO, Microsoft has predominantly been embracing open source communities. Their MVP now includes open source contributions as well.
They have recently released VS-Code editor that fits rightly for a right looking IDE for Node.js. Its light wait and installs quickly. It is a perfect one to develop ASP.NET with Node.js.
Basic Authentication using Expressjs and Nodejs Base64 encoding
Express.js gives us an out-of-box solution for ding basic authentication via its request parameter.
We all know that the callback function has request and response (req,res). So, when the user makes a request to our Node.js, we receive Header information as well in the request object. In that if 'authorization' property is set and given a value, that can be used. We can ask our REST API consumers to send username and password in Base64 encoding as the value of this authorization property.
The base64 coded value can be obtained using https://www.base64encode.org/
For us, the validation is if username is equal to password we give the data to the consumer of the API. In real life scenario, we might do it against the data values in our DB.
Let us take a look at the code. The code itself has explanation. Please drop a comment /share this post if you like it or even if you have any queries. See you soon with one more tutorial.
Ultimate Node.js Beginner Tutorial - Introduction
I know you would have read a lot of blogs /
articles and pages on internet and watched hours of Node.j sessions on YouTube
and coursera. You might have landed on this page praying that at least this
page should do some justice to your relentless and tiring search for a better
article.
If the above statements are true, even to a certain
extent then trust me, you are at the right place. In the series of articles on
my blog, I will make sure you leave from here with stomach full of knowledge on
Node.js and your nerves full of enthusiasm and your heart full of belief and
your neurons full of excitement to develop the next Node.js application.
Let’s jump right away on the task. We have a big
task ahead and no time should be wasted.
Here is the Video for you guys. There is more in it on top of the content that is in this post. Listen to it, subscribe and share.
Building
REST API using Node.js
Question: What is Node.js?
Answer:
·I know it might be boring to read again
and so I will keep it plain and simple.
·Node.js is a framework that makes us
JavaScript run on server.
·It is built on top of Google Chrome’s
V8 engine which is the fastest JavaScript engine till date.
·Node.js is single threaded.
·Node.js helps us do Asynchronous I/O
operations.
·Node.js does I/O operations that are
non-blocking.
·Node.js helps us build not just
websites but also REST APIs that are easy to design and are faster than web
services or WCF services.
Question: How Node.js is fast if it is single
Threaded and how does it do non-blocking Asynchronous I/O operations?
Answer: Ahh.!!! I can see the expression on your
face. Spot On! That’s what I said, right.
Node.js under the hood takes help of the C++
libraries called as libev and libuv and libeio to make this work.
Libuv is the module from C++ that makes Node.js do
all the talking in Asynchronous I/O operations.
Below
is the picture to help you understand better.
Question: Then what about the non-blocking aspect?
Answer: As you know JavaScript engine has the event
loop that takes care of waiting functions and processes. It is the same thing
that is used here. Because that is from V8 engine, it is faster than anything
else.
To understand better, here is the picture of the
same
Nodejs REST API Authentication using Passport.js
Hi All, I know its been a while that I have written an article here. However, I have not waster a single day. I was into writing concepts on JavaScript Module pattern and JavaScript Prototype Pattern. I will soon put up all the content explaining JavaScript Prototype pattern and Prototype by default in great detail here. Coming back to the current article, Nodejs , the serverside JavaScript framework is famous for writing REST APIs. It is also famous for writing webapps (web pages) using ejs/ jade/hapi etc as the web frameworks. In this article, I will help you out writing an API in node and authenticating it using passportjs.org's passport-http basicStrategy method. In real world scenarios, it is advised to use SSL to implement authentication. So, lets jump into the code an build an API. 1. do the below three npm first in your project.
npm express
npm passport
npm passport-http.
2. Now, define the require statements. 3. define a port where the API is exposed for use and the app listens to. 4. Let the app know that it has to use passport. 5. Initialize the passport 6. Define the BasicStrategy via the passport-http. 7. Let passport know that it has to use BasicStrategy to do the authentication 8. As per the documentation, BasicStrategy takes three arguments. They are the input username, password and a callback named 'done'. 9. 'done' lets the passport know if there was a success or a failure during the authentication. 10. Based on the result of the authentication, that is injected to the express framework via app that uses this passport to allow the user to the next level of query using the next() call of the express. 11. For http, because the APIs are session less, the session is false in the code. 12. In our example, we return true when username and password are same. However, in real world example, the username and password are to be cross verified against your database user table details. 13. In our example, once the user is authenticated, we display some static data. In real world, it is some data from database. Complete code is below. You can also look at the complete code here. So there you go, below are the screenshots when i run the application. Hope this helps. I will soon upload the application to github. Thank you so much. See you soon with more JavaScript, C# and Node.js code samples and code snippets.
Using postman app for chrome, i was trying to post to my node js application listening to port 3001.
however, all the body of the data is sent as empty.
Because of this, the only data that is getting saved in mongodb using the mongoose connection was as below:
{
"_id": "542e4ea1480f9abc13239976",
"__v": 0
}
The issue with the code was that the body parser was placed at the end after the mongo connection was created and just above the app.listen(3001) line
When i changed the code to the top of the file just after the line