Showing posts with label NodeJS. Show all posts
Showing posts with label NodeJS. Show all posts
NodeJS Webhooks

NodeJS Webhooks

npm install node-webhooks --save 

// Initialize WebHooks.
var WebHooks = require('node-webhooks')
// Initialize webhooks from database
var webHooks = new WebHooks({
    db: './webHooksDB.json', // json file that store webhook URLs
    httpSuccessCodes: [200, 201, 202, 203, 204],
})
// Alternatively, initialize webhooks with object

webHooks = new WebHooks({
    db: {"addPost": ["http://localhost:9100/posts"]},
})
// sync instantation - add a new webhook called 'name1'
webHooks.add('name1', 'http://127.0.0.1:9000/prova/other_url').then(function(){
    // done
}).catch(function(err){
    console.log(err)
})
// add another webHook
webHooks.add('name2', 'http://127.0.0.1:9000/prova2/').then(function(){
    // done
}).catch(function(err){
    console.log(err)
});
// remove a single url attached to the given shortname
// webHooks.remove('name3', 'http://127.0.0.1:9000/query/').catch(function(err){console.error(err);})
// if no url is provided, remove all the urls attached to the given name
// webHooks.remove('name3').catch(function(err){console.error(err);})
// trigger a specific webHook
webHooks.trigger('name1', {data: 123})
webHooks.trigger('name2', {data: 123456}, {header: 'header'}) // payload will be sent as POST request with JSON body (Content-Type: application/json) and custom header
NodeJS Setup in HTTP Server or Local Server

NodeJS Setup in HTTP Server or Local Server

NodeJS Setup in HTTP Server / Local Server Step 1 - Download and Install NodeJS
download the latest stable release of NodeJS from https://nodejs.org
Step 2 - Install http-server package from npm Open command prompt / command window and enter the following
npm install -g http-server
Step 3 - Start web server from the directory
cd \projects\angular-form-example
Step 4 - Start server with following command:
http-server
You should see something like D:\project\angular-form-example>http-server Starting up http-server, serving ./ Available on: http://http://70.35.195.158:8080 http://127.0.0.1:8080 Hit CTRL-C to stop the server Open your browser and enter the following address http://localhost:8080
Mongo DB connect with NodeJS

Mongo DB connect with NodeJS



To start Mongodb:
1.create folder named( data ) and include that folder below.
2.D:\Mongodb\bin> mongod.exe --dbpath “D:\data”
Another cmd
3. mongo.exe

Function:
1..To create database:
use DATABASE_NAME
2..insert() 
>db.COLLECTION_NAME.insert(document)
3..Update() 
>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
4..remove() 
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
5..To check data
>db.dbname.find().pretty()

To run Nodejs:
Go to the folder path and
1. npm install
2. npm i -g @angular/cli
3. ng serve or npm start or npm run dev
Node JS Pagination using in Mean Stack ( Node js mongodb pagination )

Node JS Pagination using in Mean Stack ( Node js mongodb pagination )



Nodejs Pagination

Ejs file[Html page] :
Node js mongodb pagination

     <% if (pageCount > 1) { %>
      <ul class="pagination">
<% if (currentPage > 1) { %>
                                <li><a href="/userdetails?page=1">&laquo;</a></li>
                <% } %>
                <% var i = 1;
                if (currentPage > 5) {
                                i = +currentPage - 4;
                } %>
                <% if (i !== 1) { %>
                                <li class="disabled"><a href="#">...</a></li>
                <% } %>
<% for (var j = i; j<=pageCount; j++) { %>
                <% if (currentPage == j) { %>
                                <li class="active"><span><%= j %> <span class="sr-only">(current)</span></span></li>
                <% } else { %>
                                <li><a href="/userdetails?page=<%= j %>"><%= j %></a></li>
                <% } %>
                <% if (j == (+currentPage + 4)) { %>
                                <li class="disabled"><a href="#">...</a></li>
                <% break; } %>
                <% } %>
<% if (currentPage != pageCount) { %>
                                <li><a href="/userdetails?page=<%= pageCount %>">&raquo;</a></li>
                <% } %>
   </ul>  <% } %>


    Routes.js :
         

               var totalRec = 0,
                pageSize  = 5,
                pageCount = 0;
                var currentPage = 1;
                app.get('/userdetails',  function (req, res) {
                                Register.find({}).exec(function(err, Register) {
                                   if (err) throw err;
                                                totalRec      = Register.length;
                                                pageCount     =  Math.ceil(totalRec /  pageSize);
                                                if (typeof req.query.page !== 'undefined') {
                                                                currentPage = req.query.page;
                                                }
                                                var start = 0;
                                                if(currentPage >1){
                                                                start = (currentPage - 1) * pageSize;
                                                }
                                                var data = [];
                                                for(var i=start;i<start+pageSize;i++) {
                                                                if(i < totalRec){
                                                                                data.push(Register[i]);
                                                                } else {
                                                                                break;
                                                                }
                                                }
                            res.render('userdetails.ejs', { "Register": Register, data:data, pageSize: pageSize, pageCount: pageCount,currentPage: currentPage});
                                   });            
                    });
      

Pageviews from the past week