AysadKozanoglu
AysadKozanoglu
Aysad Kozanoglu
25 Snippets  (23rd place)
Published
1 Channel
Created
4 Channels
Following
3 points  (21224th place)
Reputation
Top 5% Publisher
Serious Code Generator
Senior Code Generator
Senior Popular Coder
Junior Autobiographer
Senior Autobiographer
Serious Famous Coder
Senior Famous Coder
Serious Wise Coder
Senior Famous Coder

Recent Snippets See all snippets by AysadKozanoglu

public by AysadKozanoglu  1917  9  3  0

nodejs mongoClient http server json Api

nodejs mongoClient http server json Api : nodejs_mongoClient_http_server_json_Api.js
//process.env.NODE_ENV = 'production';
// @ autho Aysad Kozanoglu
// email aysadx@gmail.com
// install first:
// npm install simple-node-logger colors express node-uuid mongodb

var log = require('simple-node-logger').createSimpleLogger('log-httpApirest.log');
log.setLevel('info');
var colors = require('colors');
var express = require('express');
var app = express();
var nodeUuid = require('node-uuid');
var port = 8084;
var queryLimit =20;
var mongodb = require('mongodb');
var MongoClient = require('mongodb').MongoClient;
var db;

var mode = process.env;

// print process.argv
//process.argv.forEach(function (val, index, array) {
//  console.log(index + ': ' + val);
//});

var args = process.argv.slice(2);
//console.log(args[0]);
if (args[0])
	port = args[0];

function logger(txt){
//  console.log(txt);
    log.info(txt);
}

logger(mode.NODE_ENV);

// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/testdb", function(err, database) {
  if(err) throw err;

  db = database;
  // Start the application after the database connection is ready
  app.listen(port);
  logger("Listening on port "+port);
});

app.get('/', function (req, res) {

    logger( Date.now());
  //  res.end( Date.now());

})

app.get('/cihazlar', function(req, res) {
	res.set({ 'content-type': 'application/json; charset=utf-8' });
    var collection = db.collection('users');
    collection.distinct('device', function(err, docs) {
        res.json(docs);
//		db.close();
    });
})

app.get('/goster/say', function(req, res) {
	res.set({ 'content-type': 'application/json; charset=utf-8' });
    var collection = db.collection('users');
    collection.count().then(function(count) {
        res.json(count);
//		db.close();
    });
})

function logger(txt){
//	console.log(txt);
	log.info(txt);
}

app.get('/goster/:device', function (req, res) {
	//  http://...goster/htc
	res.set({ 'content-type': 'application/json; charset=utf-8' });
	var dev = req.params.device || "htc";
    var collection = db.collection('users');
    collection.find({"device":dev}).limit(queryLimit).sort({_id:-1}).toArray(function(err, 
docs){
        res.send(docs);
//		db.close();
    });
	logger(dev);
})


var lati,long,dev,uuid;
app.get('/api/:long/:lati/:device/:uuid/:adress', function(req, res) {

    lati = req.params.lati;
    long = req.params.long;
    dev = req.params.device;
    uuid = req.params.uuid;
	adress = req.params.adress;
    device = req.params.device;
    res.set({ 'content-type': 'application/json; charset=utf-8' });

	if(lati && long && dev && uuid){
		var collection = db.collection('users');
		var toInsert = {
						'long':long,'lat':lati,'device':dev,'uuid':uuid,
						'NodeID':nodeUuid.v4(),'adress':adress
						};
		collection.insert(toInsert, {w:1}).then(function(count) {
			res.json({"status":"true"})
//			db.close();
		});
		toLog = long +" "+ lati +" "+uuid+" "+colors.yellow(dev)+" "+adress;
		logger(toLog);
	}
})


//
//var server = app.listen(port, function () {
//
//   var host = server.address().address
//   var port = server.address().port
//
//   console.log("listening at http://%s:%s", host, port)
//
//})


;

public by AysadKozanoglu  128896  1  2  0

request flood protection rate limiting nginx

request flood protection rate limiting nginx: nginx-rate_limit_flood_protect.conf
# To enable rate limiting simply add the following line to the top-level of your config file:

# 1 request / second
limit_req_zone $binary_remote_addr zone=login:10m rate=1r/s;

# Then apply it to a location by adding a rate  limiting burst to your server block:

location /account/login/ {
    # apply rate limiting
    limit_req zone=login burst=5;

    # boilerplate copied from location /
    proxy_pass http://myapp;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
}


;

public by AysadKozanoglu  1992  0  3  0

simple example together working of nodejs mongoDB express

simple example together working of nodejs mongoDB express: nodeJs_express_service.js
// Aysad Kozanoglu
//before use npm install express

var express = require('express');
var app = express();
var port = 8000;

app.get('/', function (req, res) {
  res.send('die Welt ist grösser als nur die Fünf ;)!');
});

app.listen(port, function () {
  console.log(' Service lauscht auf dem port '+8000);
});


;

public by AysadKozanoglu  8155  2  3  0

difference between onload, readyState, and DOMContentLoaded to see it look on javascript console log

difference between onload, readyState, and DOMContentLoaded to see it look on javascript console log : readyState_onLoad_example.html
    <!DOCTYPE html>
    <html>
      <head>
        <script>
        // best way for me  with experience
            if ('addEventListener' in document) {
                document.addEventListener('DOMContentLoaded', function() {
                   //your code here...
                }, false);
            }        
        //-->
        
          window.onload = function () {
            console.log(''+(+new Date)+': Onload fired');
          };
     
          document.onreadystatechange = function () {
            console.log(''+(+new Date)+': Ready state changed'); 
            if (document.readyState == "complete") { 
              console.log(''+(+new Date)+': Ready = Complete'); 
            }
          };
     
          window.addEventListener("DOMContentLoaded", function () {
            console.log(''+(+new Date)+': DOM Content Loaded');
          });
        </script>
      </head>
      <body>
        <h1>Test</h1>
        <p>To see the difference between <code>onload</code> and <code>readyState == "complete"</code>
          and <code>DOMContentLoaded</code>.</p>
        <p>Run me with Firebug or Chrome Dev Tools, and cache disabled, and watch the console.</p>
        <hr />
        <img src="http://i.imgur.com/owuVS.gif" />
        <img src="http://i.imgur.com/1sE1n.jpg" />
        <img src="http://i.imgur.com/A2s1e.jpg" />
        <img src="http://i.imgur.com/BaFin.jpg" />
      </body>
</html> 


;

public by AysadKozanoglu  1174  8  4  0

nginx configuration file, (do not log robots.txt and favicon.ico requests)

nginx configuration file, (do not log robots.txt and favicon.ico requests): do_not_log.conf
# Designed to be included in any server {} block.
location = /favicon.ico {
	log_not_found off;
	access_log off;
}

location = /robots.txt {
	allow all;
	log_not_found off;
	access_log off;
}


;