Showing posts with label nodeJS. Show all posts
Showing posts with label nodeJS. Show all posts

Thursday, December 31, 2020

Node express up and running in 5 minutes

Follow the guidelines here: https://expressjs.com/en/starter/generator.html

All in all, from command prompt:
  1. npx express-generator my-app
  2. cd my-app
  3. npm install
  4. npm start

 

Monday, April 13, 2020

To understand module exports

As the article header says, it was a good effort by the author to help the readers to understand Node.js module exports:

https://stackify.com/node-js-module-exports/

Tuesday, March 10, 2020

OKTA

Get quick and robust authentication by adding one of OKTA's SDKs to your app or API service.

https://developer.okta.com/docs/

Build user registration with NodeJS, React, and Okta:

https://developer.okta.com/blog/2018/02/06/build-user-registration-with-node-react-and-okta

may be another good read??: https://developer.okta.com/code/react/okta_react/

Monday, March 9, 2020

Build Rest API with NodeJS

Ain't this article cute? (with all the pictures, icons, emojis):

https://dev.to/lenmorld/quick-rest-api-with-node-and-express-in-5-minutes-336j (my 5 stars to this author)

Golden statement from the author: Rest API means providing an API to clients via HTTP Methods

https://www.positronx.io/build-secure-node-js-mongodb-express-restful-api-from-scratch/

Also, other useful links while practicing these exercises:

https://medium.com/@alexishevia/using-cors-in-express-cac7e29b005b

All in all, to overcome the CORS issue while playing around with express & rest api:

npm install cors

In server.js...
let cors = require('cors')
server.use(cors())

This would set the Access-Control-Allow-Origin: *

https://alligator.io/react/axios-react/

Saturday, September 16, 2017

REST in peace

Say, you have some data in a database and you want to manipulate it. You do that through one of the CRUD operations. But you need an interface to perform these actions like a command prompt where a CLI (Command Line Interface) is available for that database, or a GUI interface, etc.

Suppose you want to carry out these data manipulations remotely, how do you do it? Again, you either VPN into the machine where you have access to this database and carry out the operations. Or, if you have an online application, you can access on a browser and carry out these operations.

What if you don't have an online application but just got couple of URLs that could help you to manipulate your data thru http protocol from a remote machine?

So you build an application that provides these URLs so that anybody who wants to get the data, insert the data, update the data, or delete the data can use them thru http access. You build one URL that helps user to insert/get/delete the data based on the HTTP request method. This URL is called as a Representational State Transfer Service or a REST Service.
  1. What is REST stands for?
  2. REST stands for Representational State Transfer.
    1. A REST Service is used to manipulate the state of data through HTTP protocol.
    2. Following is the match between HTTP requests and CRUD actions.
      1. HTTP request POST - Create
      2. HTTP request GET - Read
      3. HTTP request PUT - Update
      4. HTTP request DELETE - Delete
  3. So how do you build a REST Service?
    1. You can build a REST Service in multiple ways some of which include a Spring boot application, a NodeJS application, etc.
    2. All you got to remember while building a REST service is to obey the HTTP request types. So when a POST REST service is made for example, your application should receive the data and do some create/insert process.
  4. What is the Spring Boot REST service application architecture looks like?
    1.  There will be a REST Controller that declares the name of the REST service and attaches it to a service java class that would perform the CRUD operations.
  5. Example sites to build a REST service using NodeJS and also Spring Boot?
    1. Spring Boot based Rest service application
    2. NodeJS, Express, MongoDB based Rest Service application







Wednesday, October 19, 2016

What is "export default" in JavaScript?

ES6 is the first time that JavaScript has built-in modules. ES6 modules are stored in files.

The "export" statement is used to export functions, objects or primitives from a given module(or file).

There are two types of export available:

  • Named export - useful to export several values from a given file.
  • Default export - there can only be a single default export from a given file. 
If a module defines a default export, then you can import the default export by omitting the curly braces.

Read more:

Sunday, September 18, 2016

Node basics

How to parse command line arguments in nodejs?

>node test.js hi hello cool

test,js
var cmdArgs = process.argv.slice(2);
console.log(cmdArgs); //prints ["hi","hello","cool"]

How to write a reusable module and how to import it in test.js?

myreusablemodule.js
module.exports = {
      sayHello : function(){
           console.log("Hello, how u doing?");
     }
};

test.js
var rModule = require("./myreusablemodule"); //returns whatever is assigned to module.exports
console.log(rModule.sayHello());

>node test.js

Explain about event driven programming in nodejs?

In an event driven programming, there is a main loop that listens for events, and then triggers a callback function when one of these events is detected.
Read more here: https://www.tutorialspoint.com/nodejs/nodejs_event_loop.htm

Give an example of event driven programming in nodejs?

All you need to remember is on and emit.

test.js
var events = require("events");
var eventsEmitter = new events.EventEmitter();
var mycustomeventassocfunction = function(){
      console.log("i am the response from mycustomeventassocfunction");
};
eventsEmitter.on("mycustomevent",mycustomeventassocfunction);

eventsEmitter.emit("mycustomevent");

>node test.js

How does the async functions work in nodejs?

Events and callbacks help nodejs to support concurrency.
database.query('something', function(err, result) {
  if (err) handle(err);
  doSomething(result);
});
The async function query takes callback function as the last parameter and the callback function takes err as the first parameter.
Read more here: https://www.tutorialspoint.com/nodejs/nodejs_event_loop.htm

What are the global objects in nodejs?

You don't have to require/include the global objects in nodejs programs. For example, console, process, __filename, __dirname, etc.

Read more here: https://www.tutorialspoint.com/nodejs/nodejs_global_objects.htm

test.js
console.log(__filename);

>node test.js

Tuesday, March 1, 2016

NodeJS for Beginners

I come across the following site where it was explained in simple about node, node custom modules, making database connection from a node module, etc. A must read...

http://code.tutsplus.com/tutorials/nodejs-for-beginners--net-26314

Monday, February 29, 2016

NodeJS and PhoneGap

Phonegap is a client-side solution only with JavaScript/CSS/HTML running on the browser-app of the phone. The JavaScript Phonegap API talks to the native phone interface and browser interface which gives you options to work natively and as a normal web page would with enhanced permissions. Node.js would only serve you as a data connection for JSON or whatever else you would need to pull/push with a network call.

Reference: http://stackoverflow.com/questions/14513029/phonegap-with-node-js

Sunday, April 5, 2015

module.exports in Node.js

Suppose you want to write a module, say a functions module and access it in another module, say main.js, how are you going to do it in Node.js?

Real simple!

Create two .js files under C:\nodetest folder: functions.js, main.js

functions.js
module.exports = { addNumbers : function() { var total = 0; for (var a = 0; a < arguments.length; a++){ total += parseInt(arguments[a]); } return total; }, findArea : function(r){ return 3.142 * r * r; } };


or 


functions.js:
this.addNumbers = function() { var total = 0; for (var a = 0; a < arguments.length; a++){ total += parseInt(arguments[a]); } return total; }; this.findArea = function(r){ return 3.142 * r * r; };

main.js
var functions = require('./functions.js'); console.log(functions.addNumbers(1,2)); console.log(functions.findArea(2));

Run the main.js from command prompt!

C:\nodetest>node main

Reference:

http://www.sitepoint.com/understanding-module-exports-exports-node-js/

Friday, February 20, 2015

Using NodeJS, express, and MongoDB

Using NodeJS, express, and MongoDB stack? The following web site gives an introduction about how to install and run a full stack using these technologies in easy steps. Cool!
http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/

Saturday, March 1, 2014

3/1/2014

Building your own web server with nodejs in 5 minutes

Well, it took me almost a week to get back to my blog :)

Couple of things I want to add to my blog before I forget! I mentioned about NPM, the node package manager that helps you to get the new modules. So how do I request for a new module using npm?

Psst! I stumbled upon this blog http://blog.modulus.io/absolute-beginners-guide-to-nodejs which got cool things that you can do with node.js. And I am installing express module (of course, using npm) to build my own web server.

I have my web project 'npj' created under: C:\Development. So basically 'npj' is a folder under C:\Development that has all my JavaScript, CSS, images folders and files.

Now request for the module you are looking for:

C:\Development>npm install express

npm is going to install the express module in there, that's it!

Now, let's go ahead and create a web site and browse it using your own web server that you can build with node. Create a file nodeWebServer.js under C:\Development.

           var express = require('express');

           var app = express();

           app.use(express.static(__dirname + '/npj'));

           app.listen(8080);

Four lines of code and my server is ready! Run my server as below:

C:\Development>node nodeWebServer.js

Note: Now you are using node.js to run your newly created web server. So node.js is busy running it so you CAN NOT access it from command prompt any more unless you stop your web server.




Sunday, February 23, 2014

2/23/2014


More on nodejs

Well, I thought of jumping right into tuts+ tutorial on node.js but before that I wanted to know more details about node.js. Things like why should I use node.js, and how is it going to help me and where? And following is what I found:
  • Node.js is a command line tool (or some people call it as a JavaScript environment) that helps to run JavaScript programs.
  • You can build a regular web server with node.js in no time (rather surprising as I remember the days when I was looking to set up a local web server like IIS, Apache tomcat, etc. to help me develop some html stuff on my local PC).
  • V8 is Google's open source JavaScript engine and node.js is built on top of V8.
  • When you build a web server with node.js, you can share code between browser and backend
  • You can even connect to a database like mysql
  • Node.js uses a module architecture and this helps to build complex applications easily.
  • To effectively run a company (either a micro company or a major corporation), you need a manager to manage the show. Node.js is no less. It has a package manager, called Node Package Manager (NPM) that helps you to install new modules.
  • You want to write a JavaScript program and run it using Node.js, you basically need the modules that are provided by node.js. You can check for any new modules available on Github. How to install new modules using the node package manager?
  • npm install new_module



Saturday, February 22, 2014

2/22/2014

2/22/2014

About node.js

These days everybody talks about node.js. Some of them boast about what cool things they did with node.js. Well, I need to catch up with these guys but I don't want to spend too much time learning node.js. So I set out a challenge myself to find out more about node.js in a short period of time.

So I downloaded node.js and all ready to go!

My first experiment with node

  1. On my windows PC, I created a file test.js under C:\node-examples folder
  2. I added the following line in test.js:
  3. console.log("testing node");
  4. From command prompt, I ran the command "node test.js" and "testing node" came up.
  5. C:\node-examples>node test.js
  6. Viola! I was able to run my JavaScript file from command prompt using node.
My second experiment with node
  1. I modified test.js so that now it has the following content:
  2. function printLine(){
           console.log("printing from node");
    }
    console.log(printLine());
  3. From command prompt, I ran the command "node test.js" and "printing from node" came up. Nice!!
  4. C:\node-examples>node test.js
Notes:
There is this site http://code.tutsplus.com/tutorials/nodejs-for-beginners--net-26314 where they have a simple but good tutorial on node.js! I am going to dive into that right away!