Tuesday, 1 August 2017

JSON : simple generate json from string





NODE-JSON

STEP-1 : Download and Install Node.js from the official Node.js web site: https://nodejs.org.

STEP-2 : Create a file – Name : Server.js (Use any name with extension “.js” ).

STEP-3 : Open command prompt  à Specify the server.js path
Like – “ cd Desktop\Node-Server”

STEP-4 :  If multiple version of node installed in PC then –
Command – “nvm use 4.2.4”
Description : nvm stand ‘Node Version Manager’.

STEP-5 : Initialize npm (Node Package Manager)
Command – “npm init”
Description : package.json file is generated, where server.js file is exist.

STEP-6 : Now install npm express.
Command “npm install --save express”
Description : --save means – “Hey, install npm into pacage.json”. express tool is use to build api.

STEP-7 : Now, install npm body-parser.  Body-parser tool is use to parse incoming request and return to json.
Command “ npm install --save body-parser”
Note :  node_module folder is created, where your server.js exist. Inside package.json file, You can see express and body-parser version.

STEP-8 : server.js file

var express = require('express');
var bodyparser = require('body-parser');
var app = express();

app.all('/*', function(req, res, next){
            res.header("Access-Control-Allow-Origin", "*");
            res.header("Access-Control-Allow-Headers", "X-Requested-With", "Content-Type, Accept");
            res.header("Access-Control-Allow-Methods", "POST, GET");
            next();
});

app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended: false}));

var tutorials = [{
            id:1,
            title: "This is title",
            description: "Data detail shown here",
            image: "Not yet defined"
},
{
            id:2,
            title: "This is title2",
            description: "Data detail shown here2",
            image: "Not yet defined2"
}];

app.get('/tutorials', function(req, res){
            console.log("GET from server");
            res.send(tutorials);
});

app.listen(8080);

STEP-9 : Now, run it in browser.

Output :  [{"id":1,"title":"This is title","description":"Data detail shown here","image":"Not yet defined"},{"id":2,"title":"This is title2","description":"Data detail shown here2","image":"Not yet defined2"}]

STEP-10 :  Your srver available globally not for your project.
Command “npm install –g nodemon”

STEP-11 :  Restart server

Command “ nodemon server.js”

Monday, 2 January 2017

Hello World



After Setup Environment Variable...

 Let's create a Hello app

1. Open Notepad and paste below code

console.log("Hello Node!");

2. Save it with name of "hello.js" or you can choose
3. Now open command prompt

**************************************************
 node  folder_path\file_name.js
*************************************************

Node.js Environment setup



The source code written in source file is simply javascript. The Node.js interpreter will be used to interpret and execute your javascript code.
Node.js distribution comes as a binary installable for SunOS , Linux, Mac OS X, and Windows operating systems with the 32-bit (386) and 64-bit (amd64) x86 processor architectures.
Installation
1. Download latest version of Node.js installable archive file from Node.js Downloads.
2. Install the setup as normal
3. Copy the installation folder path. In my case - C:\Program Files\nodejs
4. Goto Environment  variable - 
My Computer ( Window 7 and below ) / This PC ( Window 10 )  --->  right click  --->
Properties  --->  Advance System Setting ---> Advance Tab --->  Environment variable ---> path ---> paste your node.js folder path ( In my case : C:\Program Files\nodejs ) ---> Ok