Friday, 17 March 2017

Node js Tutorial for beginners with examples - page 4

Node js Tutorial for beginners with examples - page 4

Question: How to print a stack trace in Node.js?
console.trace("print me")



Question: How can I get the full object in Node.js's console.log()?
const util = require('util');
console.log(util.inspect(myObject, false, null))



Question: How to use jQuery with Node.js?
First instal the jquery then use as following.
require("jsdom").env("", function(err, window) {
    if (err) {
        console.error(err);
        return;
    }

    var $ = require("jquery")(window);
});



Question: How to change bower's default components folder?
Create a .bowerrc file in root and add following code.
{
  "directory" : "public/components"
}



Question: How to encode base64 in nodeJS?
console.log(new Buffer("hello").toString('base64')); //aGVsbG8=



Question: How to decode base64 in nodeJS?
console.log(new Buffer("aGVsbG8=", 'base64').toString('ascii')); //hello



Question: How to get listing of files in folder?
const testFolder = './files/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
  files.forEach(file => {
    console.log(file);
  });
})



Question: How do you extract POST data in Node.js?
app.use(express.bodyParser());
app.post('/', function(request, response){
    console.log(request.body.user.fname);  //print first name
    console.log(request.body.user.lname); //print last name
});



Question: How to remove file nodeJS?
var fs = require('fs');
var filePath = '/folder/arun.docx'; 
fs.unlinkSync(filePath);



Question: How to access the GET parameters after ? in Express?
var email = req.param('email');


Question: How to copy file in nodeJS?
var fs = require('fs');
fs.createReadStream('existing.txt').pipe(fs.createWriteStream('new_existing.txt'));



Question: How to append string in File?
var fs = require('fs');
fs.appendFile('message.txt', 'data to append', function (err) {

});



Question: How to get version of package?
var packageJSON = require('./package.json');
console.log(packageJSON.version);



Question: What is express js?
Express.js is a NodeJS framework.
It is designed for building single-page, multi-page, and hybrid web applications.


Question: How to Send emails in Node.js?
You can use node-email-templates.
https://github.com/crocodilejs/node-email-templates


Question: How to get Ip Address of client?
request.connection.remoteAddress



Wednesday, 15 March 2017

How to Send iOS push notification from PHP

How to Send iOS push notification from PHP?

PHP Code
//device token
$deviceToken = "a58c9e7406c4b591eda8f192027d00ef82cdba361821f693e7b8ac9cb3afbc61";

//Message will send to ios device
$message = 'this is custom message';

//certificate file
$apnsCert = 'pushcert.pem';
//certificate password
$passphrase = 1234;

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);


// Create the payload body
$body['aps'] = array(
    'badge' => +1,
    'alert' => $message,
    'sound' => 'default'
);

$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered amar' . $message . PHP_EOL;

// Close the connection to the server
fclose($fp);



Question: What is full form of APNS?
Apple Push Notification Service


Question: What is device token in ios?
Device token is an identifier for the Apple Push Notification System for iOS devices.


Question: What is apnsHost for Development and LIVE?
Development Server
ssl://gateway.sandbox.push.apple.com:2195

Production Server
ssl://gateway.push.apple.com:2195



Question: From where we can test the APN Host?
http://pushtry.com/


Question: What port need to enable for APNs notification?
2195
2196