/*
* Please implement this method to return the sum of the two elements closest to zere. If there are two elements equally close to zero like -2 and 2.
* consider the positive element to be "closer" to zero than the negative one.
*
*
*/
function sumOfTwoElementsClosestToZero(arr) {
var length = arr.length;
var first = arr[0];
var second = arr[0];
for (var i = 0; i < length; i++) {
if (arr[i] == 0) {
continue;
}
if (abs(first) > abs(arr[i]) || (abs(first) == abs(arr[i]) && arr[i] > 0)) {
first = arr[i];
}
}
for (var i = 0; i < length; i++) {
if (arr[i] == 0) {
continue;
}
if (abs(arr[i]) > abs(first) && (abs(second) > abs(arr[i]) || abs(second) == abs(arr[i]) && arr[i] > 0)) {
second = arr[i];
}
}
return (second + first);
}
function abs(num) {
return num < 0 ? -num : num;
}
console.log("test 1 : " + (sumOfTwoElementsClosestToZero([7, 8, 3, 4, 2, 8, -3, 4, -2, -100]) == 5));
console.log("test 2 : " + (sumOfTwoElementsClosestToZero([7, 8, -3, 4, 2, 8, -3, 4, -2, -100]) == -1));
console.log("test 3 : " + (sumOfTwoElementsClosestToZero([7, 8, 3, 4, -1, 8, -3, 4, -2, -100]) == -3));
console.log("test 4 : " + (sumOfTwoElementsClosestToZero([7, 8, 3, 4, 2, 8, -3, 4, -2, -100]) == 5));
console.log("test 5 : " + (sumOfTwoElementsClosestToZero([-3, -2, -1, 1, 0, 2]) == 3));
Friday, November 18, 2016
Write a method get sum of two element which closest to Zero
Here is the code in Javascript:
Sunday, October 30, 2016
How to start using DerbyDB with Netbean?
Why we need to use DerbyDB, fisrt of all , it is shipped a long with Java JDK,
If you first learn Java and want to learn How Java work with Database,
We can use DerbyDB, Because we dont need to install a new DB that reason.
Another reason, I am wonder, how to use all tools shipped by Java JDK,
First Open Netbean,
Check configuration of JavaDB Choose location of JavaDB installed on your machine, normally the
DerbyDB was shipped along with JDK
Right click on JavaDB --> Choose create new DB
After create DB success, a new database connection automatically created by Netbean,
Click on connect to
Netbean will automatically startup DerbyDB and Connect to that data.
Now database is ready for futher more steps.
If you first learn Java and want to learn How Java work with Database,
We can use DerbyDB, Because we dont need to install a new DB that reason.
Another reason, I am wonder, how to use all tools shipped by Java JDK,
First Open Netbean,
![]() |
| Check Derby database configuration |
Check configuration of JavaDB Choose location of JavaDB installed on your machine, normally the
DerbyDB was shipped along with JDK
Right click on JavaDB --> Choose create new DB
After create DB success, a new database connection automatically created by Netbean,
Click on connect to
Netbean will automatically startup DerbyDB and Connect to that data.
Now database is ready for futher more steps.
Location:
Hồ Chí Minh, Việt Nam
Saturday, October 22, 2016
Serialization and Deserialization JSON in JAVA with GSON
Hello,
Today I will give an example of using GSON to Serialization and Deserialization JSON in JAVA
When we work with JSON, there are some tasks we usually do,
Here is some code example:
Here is output of the code:
You can download source code here
Today I will give an example of using GSON to Serialization and Deserialization JSON in JAVA
When we work with JSON, there are some tasks we usually do,
- Parse Object to JSON and vice versa
- Parse Collection to JSON and vice versa
- And sometime we need to create an JSON object with dynamic field, In my example I used Map to do it.We also need to Deserialization and JSON object with dynamic field to Java Object
Here is some code example:
package com.blogspot.ducnguyen.dev.json.example;
import com.google.gson.Gson;
import java.awt.Point;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author ducnguyen
*/
public class Main {
private static Gson gson = new Gson();
public static void main(String[] args) {
System.out.println("Serialization and Deserialization an OBJECT");
Point p1 = new Point(2, 1);
String json1 = gson.toJson(p1);
System.out.println("p1 -> json1 : " + json1);
Point p2 = gson.fromJson(json1, Point.class);
System.out.println("json1 -> p2 : " + p2.toString());
System.out.println("Serialization and Deserialization a LIST");
List l1 = Arrays.asList(new Point(2, 1), new Point(3, 4));
String json2 = gson.toJson(l1);
System.out.println("l1 -> json2 : " + json1);
List l2 = gson.fromJson(json2, List.class);
System.out.println("json2 -> l2 : " + l2.toString());
System.out.println("Create object fields with MAP");
Map jsonObject = new HashMap<>();
jsonObject.put("integer", 100);
jsonObject.put("string", "hello world");
jsonObject.put("boolean", Boolean.TRUE);
String json3 = gson.toJson(jsonObject);
System.out.println("jsonObject -> json3 : " + json3);
System.out.println("Deserialization MAP");
Map jsonObject2 = gson.fromJson(json3, Map.class);
System.out.println("json3 -> jsonObject2 : "+ jsonObject2.toString());
}
}
Here is output of the code:
Serialization and Deserialization an OBJECT
p1 -> json1 : {"x":2,"y":1}
json1 -> p2 : java.awt.Point[x=2,y=1]
Serialization and Deserialization a LIST
l1 -> json2 : {"x":2,"y":1}
json2 -> l2 : [{x=2.0, y=1.0}, {x=3.0, y=4.0}]
Create object fields with MAP
jsonObject -> json3 : {"integer":100,"string":"hello world","boolean":true}
Deserialization MAP
json3 -> jsonObject2 : {integer=100.0, string=hello world, boolean=true}
You can download source code here
Location:
Thung lũng Silicon, California, Hoa Kỳ
Saturday, October 15, 2016
AngularJS - formatter and parser example
When I work with an old project built use AxonIvy ,
I got an issue with date format
In that project, If I want to send a date from Web application using AngularJS to Server Side.
I need to send a date with format
For example:
Also the Server will return the same format for date fields.
For the client side, I use an AngluarJs UI Angular-Strap ,
Which provided very nice widgets.
Unfortunately, bsDate picker only work with common dateformat like ISO , UTC or Timestamp
which look likes:
In order to make Ivy Date work, I create a new directive :
So this directive with get date object from Server and display it as normal date to View
And everytime the View change, it will return a date with Server format to Model
One of the most important thing in my code is priority,
I set priority to 1 to ensure my $formatter function with run before the $formatter of bsDatePicks, After take my time to investigate that my formatter always run after the formatter of bsDatePicker, and bsDatePicker can only work with date in common format.
Here is remainng code:
Controller:
HTML - View:
Here is live demo on Plunker
I got an issue with date format
In that project, If I want to send a date from Web application using AngularJS to Server Side.
I need to send a date with format
For example:
{
day: 31,
month: 12,
year:2016
}
Also the Server will return the same format for date fields.
For the client side, I use an AngluarJs UI Angular-Strap ,
Which provided very nice widgets.
Unfortunately, bsDate picker only work with common dateformat like ISO , UTC or Timestamp
which look likes:
"2016-10-16T04:06:15.481Z" or 1476590721627
In order to make Ivy Date work, I create a new directive :
app.directive('ivyDate', function() {
return {
require: 'ngModel',
priority: 1,
link: function(scope, elem, attrs, ngModelCtrl) {
// formats text going to user (from model to view)
ngModelCtrl.$formatters.push(function(value) {
var modelValue = scope[attrs['ngModel']];
return new Date(Date.UTC(modelValue.year, modelValue.month - 1, modelValue.day));
});
// formats text from the user (from view to model)
ngModelCtrl.$parsers.push(function(value) {
var date = new Date(value);
return {
day: date.getUTCDate(),
year: date.getFullYear(),
month: date.getMonth() + 1
};
});
}
};
})
So this directive with get date object from Server and display it as normal date to View
And everytime the View change, it will return a date with Server format to Model
One of the most important thing in my code is priority,
I set priority to 1 to ensure my $formatter function with run before the $formatter of bsDatePicks, After take my time to investigate that my formatter always run after the formatter of bsDatePicker, and bsDatePicker can only work with date in common format.
Here is remainng code:
Controller:
var app = angular.module('mgcrea.ngStrapDocs', ['ngAnimate', 'ngSanitize', 'mgcrea.ngStrap']);
app.controller('MainCtrl', function($scope, $timeout) {
$scope.simpleDate= new Date();
$scope.ivyDate= {
day: 30,
month: 11,
year: 2016
};
$scope.setDate = function() {
$scope.ivyDate= {
day: 30,
month: 11,
year: 2015
};
}
HTML - View:
<body ng-controller="MainCtrl">
<form name="datepickerForm" class="form-inline" role="form">
<div class="form-group" ng-class="{'has-error': datepickerForm.date2.$invalid}">
<label class="control-label"><i class="fa fa-calendar"></i> Normal Date <small></small></label>
<input type="text" class="form-control" ng-model="simpleDate" data-date-format="yyyy-MM-dd" data-date-type="number" data-min-date="02/10/86" data-autoclose="1" name="date2" bs-datepicker >
</div>
<hr />
<br />
<!-- Custom example -->
<div class="form-group" ng-class="{'has-error': datepickerForm.date2.$invalid}">
<label class="control-label"><i class="fa fa-calendar"></i> Ivy Date <small></small></label>
<input type="text" class="form-control" ng-model="ivyDate" data-date-format="yyyy-MM-dd" data-date-type="number" data-min-date="02/10/86" data-autoclose="1" name="date2" ivy-date bs-datepicker >
</div>
<br />
<br />
<button class="btn btn-primary" ng-click="setDate($event)"> setDate </button>
<textarea cols="14" rows="7">
{{ivyDate|json}}
</textarea>
<hr>
</form>
</body>
Here is live demo on Plunker
Labels:
angularjs,
HTML/CSS,
javascript
Location:
Hồ Chí Minh, Việt Nam
Wednesday, October 5, 2016
Java8 - Working with Base64
In JDK8, It is supported Base64 decode/encoding,
Here is an example of using Base64
Here is an example of using Base64
package my.example.java.base64;
import java.util.Base64;
import org.junit.Assert;
import org.junit.Test;
/**
*
* @author nvduc
*/
public class Base64Test {
public Base64Test() {
}
@Test
public void test() {
String origin = "Hello World";
String base64 = new String(Base64.getEncoder().encode(origin.getBytes()));
String fromBase64 = new String(Base64.getDecoder().decode(base64));
System.out.println("text Encoded : " + base64);
System.out.println("text Decoded : " + fromBase64);
Assert.assertEquals(origin, fromBase64);
}
}
You can find this example code on Github
Labels:
JAVA
Location:
Ho Chi Minh City, Ho Chi Minh, Vietnam
Thursday, September 29, 2016
CSS - Centering element
Hello,
Here is CSS using for centering element
Here is CSS using for centering element
<html>
<head>
<style>
/*display table cell*/
.container1{
width: 200px;
height: 200px;
border : 1px solid red;
display : table-cell;
vertical-align: middle;
}
.content1{
border:1px solid blue;
}
/*CSS3 with transform*/
.container2{
margin-top:50px;
width: 200px;
height: 200px;
border : 1px solid red;
position:relative;
}
.content2{
border:1px solid blue;
position: absolute;
top: 50%;
transform: translate(0, -50%);
}
/*CSS3 with transform*/
.container3{
margin-top:50px;
width: 200px;
height: 200px;
border : 1px solid red;
display: flex;
align-items: center;
}
.content3{
border:1px solid blue;
}
</style>
</head>
<body>
<h2>
Centering vertical with table style:</h2>
<div class="container1">
<div class="content1">
1. This small paragraph is vertically centered.<br />
</div>
</div>
<h2>
Centering vertical CSS3 :</h2>
<div class="container2">
<div class="content2">
2. This small paragraph is vertically centered.<br />
</div>
</div>
<h2>
Centering vertical using Flex :</h2>
<div class="container3">
<div class="content3">
3. This small paragraph is vertically centered.<br />
</div>
</div>
</body>
</html>
You can find the live demo here Plunker
Labels:
HTML/CSS
Location:
Hồ Chí Minh, 70000, Việt Nam
Wednesday, September 21, 2016
Java - Create image inside a rectangle
Here is an example put image into a rectangle, with calculation to keep full width of image:
You can find source code on Github
package my.example.image;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
public class ImageInRectange {
public static void main(String[] args) throws IOException {
createImageInRectagle(512, 1024, new File("./src/main/resources/chelsea.png"));
}
public static File createImageInRectagle(int width, int height, File image) throws IOException {
Image originImage = ImageIO.read(image);
int oImgWidth = originImage.getWidth(null);
int oImgHeight = originImage.getHeight(null);
float imageRatio = (float) oImgHeight / oImgWidth;
float rectangleRatio = (float) width / height;
BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
BufferedImage bufferedImage = ImageIO.read(image);
Graphics g2 = combined.getGraphics();
Point p = new Point(0, 0);
if (Math.abs(imageRatio - rectangleRatio) < 0.05) {
bufferedImage = scale(bufferedImage, width, height);
p.x = 0;
p.y = 0;
} else {
int minEdge = width < height ? width : height;
if (oImgHeight > oImgWidth) {
float diff = (float) oImgHeight / minEdge;
int newWidth = Math.round(oImgWidth / diff);
bufferedImage = scale(bufferedImage, newWidth, minEdge);
if (minEdge == height) {
p.x = Math.abs((width - newWidth) / 2);
p.y = 0;
} else {
p.x = 0;
p.y = Math.abs((height - minEdge) / 2);
}
} else {
float diff = (float) oImgWidth / minEdge;
int newHeight = Math.round(oImgHeight / diff);
bufferedImage = scale(bufferedImage, minEdge, newHeight);
if (minEdge == height) {
p.x = Math.abs((width - minEdge) / 2);
p.y = 0;
} else {
p.x = 0;
p.y = Math.abs((height - newHeight) / 2);
}
}
}
g2.drawImage(bufferedImage, p.x, p.y, null);
g2.dispose();
File tempImage = Paths.get("target", "ok.png").toFile();
ImageIO.write(combined, "png", tempImage);
return tempImage;
}
public static BufferedImage scale(BufferedImage imgb, int newWidth, int newHeight) {
Image image;
image = imgb.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
return toBufferedImage(image);
}
public static BufferedImage toBufferedImage(Image img) {
if (img instanceof BufferedImage) {
return (BufferedImage) img;
}
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return bimage;
}
}
You can find source code on Github
Wednesday, September 14, 2016
APACHE PDFBOX EXAMPLE WITH MAVEN
Here is an example of create a Maven project using Netbean.
In this example, I used Apache PDFBox as an example of using maven for managing Java library
Declare maven dependenies
Add Java Code:
Before running, we must build project, When we build, maven will get pdfbox library from maven repository
Right click on Main.java file, Choose Run File
We can check the result in Files view
You can check the source code on Github
In this example, I used Apache PDFBox as an example of using maven for managing Java library
1. We create a new project using Netbean:
![]() |
| New Maven project with Netbean |
![]() |
| Input the artifactId and package name |
2. Add code
Declare maven dependenies
4.0.0 com.blogspot.ducnguyen.dev pdfbox-example 1.0-SNAPSHOT jar UTF-8 1.7 1.7 org.apache.pdfbox pdfbox 2.0.0
Add Java Code:
package com.blogspot.ducnguyen.dev.pdfbox.example;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
/**
*
* @author nvduc
*/
public class Main {
public static void main(String[] args) throws IOException
{
long timestamp = System.currentTimeMillis();
String filename = "test-"+timestamp+".pdf";
String message = "Hello World at : "+ timestamp;
PDDocument doc = new PDDocument();
try
{
PDPage page = new PDPage();
doc.addPage(page);
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream contents = new PDPageContentStream(doc, page);
contents.beginText();
contents.setFont(font, 12);
contents.newLineAtOffset(100, 700);
contents.showText(message);
contents.endText();
contents.close();
doc.save(filename);
}
finally
{
doc.close();
}
}
}
3. Running
Before running, we must build project, When we build, maven will get pdfbox library from maven repository
![]() |
| Build project in NetBean |
We can check the result in Files view
![]() |
| Result after choose Run File |
Location:
Hồ Chí Minh, 70000, Việt Nam
Saturday, January 31, 2015
Chaining multiple promises
Sometimes we need to perform several asynchronous operations, no matter the order, and to be notified when they all done.
$q.all(promisesArr) can help us with that.
Assume we have N methods that return promises: async1(), ..., asyncN().
The following code will log done only when all operations are resolved successfully.
Result:
$q.all(promisesArr) can help us with that.
Assume we have N methods that return promises: async1(), ..., asyncN().
The following code will log done only when all operations are resolved successfully.
function readOneFileAsDataURL (file, scope) {
var deferred = $q.defer();
var reader = getReader(deferred, scope);
reader.readAsDataURL(file);
return deferred.promise;
};
function readManyFileAsDataURL (files, scope){
var promises=[];
for(var i = 0 ; i < files.length; i ++){
promises.push(readOneFileAsDataURL(files[i], scope));
}
return $q.all(promises);
};
/*
*Usage
*/
readManyFileAsDataURL(files, scope).then(function(allImages){
console.log(allImages.length);
});
Result:
/* *Some log for more clear "begin read image : 0" "begin read image : 1" "begin read image : 2" "end read image : 2" "end read image : 0" "end read image : 1" * /
Wednesday, November 19, 2014
MONGODB BASIC CONFIGURATION
1. Mongodb without authentication
C:\\mongodb\\bin C:\\mongodb\\data
C:\mongodb\bin\mongod.exe --dbpath "C:\mongodb\data"
or
C:\mongodb\bin\mongod.exe --dbpath="C:\mongodb\data"
Default port is 27017
2. Enable authentication
First we need to start mongoDB without authentication
C:\mongodb\bin\mongod.exe --dbpath "C:\mongodb\data"
After that run following commands :
1. Run mongo client with : mongo.exe
2. Use data base with : use admin
3. Add user and role : db.createUser({"user":"root","pwd":"123456","roles":[{"role":"readWriteAnyDatabase","db":"admin"},{"role":"userAdminAnyDatabase","db":"admin"},{"role":"dbAdminAnyDatabase","db":"admin"}]})
The object of root user(for easy to read)
db.createUser({
"user":"root",
"pwd":"123456",
"roles":[
{
"role":"readWriteAnyDatabase",
"db":"admin"
},
{
"role":"userAdminAnyDatabase",
"db":"admin"
},
{
"role":"dbAdminAnyDatabase",
"db":"admin"
}
]
});
And start with authentication mode:
#mongod.exe --dbpath="C:\mongodb\data" --auth
3. Creating window service with configuration file
mongo.cfg placed in c:\mongodb\etc\
systemLog:
destination: file
path: c:\\mongodb\\etc\\mongodb.log
logAppend: true
storage:
dbPath: c:\\mongodb\\data
journal:
enabled: true
processManagement:
windowsService:
serviceName: t2i-mongodb-server
displayName: T2i mongoDb server display name.
description: The service for MongoDatabase
security:
authorization: enabled
net:
bindIp: 127.0.0.1
port: 27017
Command to start as service
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\etc\mongo.cfg" --install
Thursday, November 13, 2014
LINUX BASIC COMMON COMMANDS
Filter result:
grep
ex: filter all processes and ports of java
ps -e | grep java
netstat -tunap | grep java
Process and infomation :
Information about process
$ls -l /proc/$PID/exe
OR
$ps -p PID -o comm=
OR
$cat /proc/PID/cmdline
Kill process
sudo kill -9 PID
User and Group:
Login
#su - {user-name}Login as root
#su -
check username
#whoami
logout
#logout
Extract & Compress file:
Extract
#tar -zxvf {file.tar.gz}
#tar -xf {file.tar.gz} -C /target/directory
#tar -xzf backup.tar.gz -C /target/directory
#unzip file.zip -d destination_folder
Download
#wget http://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz -P /target/directory
Restating the network service:
To restart the network service under RHEL / CentOS based systems, enter:
service network restartOR
/etc/init.d/network restart
Check linux kernel version number :
Open a shell prompt (or a terminal) and type the following command to see your current Linux kernel version:
uname -r
Or type the following command
uname -mrs
To print all information, enter
uname -a
Type the following command to see Linux version info
cat /proc/version
Find Distribution Version :
Type the following command
cat /etc/*release
OR
lsb_release -a
Port & Firewall :
Port and process command
netstat -tunap
If you want to open a single port
open iptables file
vi /etc/sysconfig/iptables
Add below line to open port.
-A INPUT -m state --state NEW -m tcp -p tcp --dport 143 -j ACCEPT
For multiple, you can use the following instead (or repeat the above line multiple times):
-A INPUT -m state --state NEW -m tcp -p tcp -m multiport --dports 22,80,143 -j ACCEPT
service iptables save
Disable / Turn off Linux Firewall (Red hat/CentOS/Fedora Core)
Type the following two commands (you must login as the root user)
/etc/init.d/iptables save
/etc/init.d/iptables stop
Turn off firewall on boot
chkconfig iptables off
Enable / Turn on Linux Firewall (Red hat/CentOS/Fedora Core)
Type the following command to turn on iptables firewall
/etc/init.d/iptables start
Turn on firewall on boot
chkconfig iptables on
Linux / Unix - Checking Free Disk Space:
df command examples - to check free disk space
Type df -h or df -k to list free disk space
df -h
OR
df -k
Find Files and Folders in Linux :
find / -name "java"
/: meaning is the root folder. hold systems
-name "java": all files have name is java.
Thursday, November 6, 2014
NODEJS - RUNNING A NODEJS SERVER AS A SERVICE ON LINUX
1. Download the project for testing:
The user logged in was: root
Download the demo server here
[root@homepc opt]# wget https://github.com/vanduc1102/nodejs-example/archive/daemon-bauth.tar.gz
[root@homepc opt]# tar -zxvf daemon-bauth.tar.gz
[root@homepc opt]# cd nodejs-example-daemon-bauth/
[root@homepc nodejs-example-daemon-bauth]# npm install
[root@hompec nodejs-example-daemon-bauth]# node index.js
You can open browser and test the application at localhost:3000
We had nodejs-example folder placed in /opt/nodejs-example-daemon-bauth
2. Create a service file in systemd:
Create file below in folder : /etc/systemd/system/
nodejs-example-service.service
[Unit]
Description=The nodejs-example server start
[Service]
WorkingDirectory=/opt/nodejs-example-daemon-bauth
ExecStart=/usr/bin/node index.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-example-nodejs-server
User=root
Group=root
[Install]
WantedBy=multi-user.target
Load the service at first time:
#systemctl enable nodejs-example-service
Start service:
#systemctl start nodejs-example-service
View status of the service:
#systemctl status nodejs-example-service
Stop the service:
#systemctl stop nodejs-example-service
3. Testing the service:
[root@homepc system]# systemctl start nodejs-example-service.service
[root@homepc system]# systemctl status nodejs-example-service.service
nodejs-example-service.service - The nodejs-example server start
Loaded: loaded (/etc/systemd/system/nodejs-example-service.service; enabled)
Active: active (running) since jeu 2014-11-06 19:18:02 ICT; 8s ago
Main PID: 3967 (node)
CGroup: name=systemd:/system/nodejs-example-service.service
└─3967 /usr/bin/node index.js
[root@homepc system]# ps -e | grep "node"
3967 ? 00:00:00 node
[root@homepc system]# kill 3967
[root@homepc system]# ps -e | grep "node"
4038 ? 00:00:00 node
[root@homepc system]# systemctl status nodejs-example-service.service
nodejs-example-service.service - The nodejs-example server start
Loaded: loaded (/etc/systemd/system/nodejs-example-service.service; enabled)
Active: active (running) since jeu 2014-11-06 19:18:02 ICT; 8s ago
Main PID: 4038 (node)
CGroup: name=systemd:/system/nodejs-example-service.service
└─4038 /usr/bin/node index.js
[root@homepc system]#
As you can see, After we kill the process, It will automatically create new one.
Thursday, October 30, 2014
nodejs - secured server with https connections
Previous post
Checkout
Checkout the code at github
If you can use command below for checkout and run the app.
vanduc@VGN-FZ290E:~/test2$ git clone --depth=10 https://github.com/vanduc1102/nodejs-example.git vanduc@VGN-FZ290E:~/test2$ cd nodejs-example/ vanduc@VGN-FZ290E:~/test2/nodejs-example$ git checkout -f 4-https vanduc@VGN-FZ290E:~/test2/nodejs-example$ npm install vanduc@VGN-FZ290E:~/test2/nodejs-example$ node index.js
HTTPS configuration
For configuration HTTPS connection we need a key and self signed certification to make the SSL work.
Creating key/certificate pair very easy with only one line, if you already installed OpenSSL
openssl req -subj '/CN=localhost/O=mycompany/C=VN' -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout server.key -out server.crt
Code for HTTPS connection:
var options = {
key: fs.readFileSync('./ssl/server.key'),
cert: fs.readFileSync('./ssl/server.crt')
};
var server = https.createServer(options,app).listen(serverConfigure.sslPort, function () {
var address,
host = serverConfigure.host ? serverConfigure.host : 'localhost',
port = serverConfigure.sslPort;
address = host + ':' + port;
console.log('HTTPS server is listenning on localhost:3443');
});
We will create HTTPS server by add option, the option is about the path of key and certificate,
the server will create with the option for SSL connection , express server by app variable.
HTTPS auto redirection
In order to protect our server (users still can access our server by HTTP connection). We need to redirect the user request to HTTPS connection.There is some ways to make HTTP auto redirection
- inside nodejs code.
- configure firewall to auto forward.
function requireHTTPS(req, res, next) {
if (!req.secure) {
var addressSSL,
host = serverConfigure.host ? serverConfigure.host : 'localhost',
port = serverConfigure.sslPort;
addressSSL = host + ':' + port;
return res.redirect('https://' + addressSSL + req.url);
}
next();
}
if (serverConfigure.httpsAutoRedirection) {
app.use(requireHTTPS);
}
If the option httpsAutoRedirection in serverConfigure file is true, we will check every request to the server and redirect the request to HTTPS port.
Tuesday, October 21, 2014
NODEJS - Basic Access Authentication
Previous Post
This article is about how to do basic authentication with NodeJS.
Checkout my project on GitHub. For one who never checkout my project before.
vanduc@VGN-FZ290E:~/test2$ git clone --depth=10 https://github.com/vanduc1102/nodejs-example.git vanduc@VGN-FZ290E:~/test2$ cd nodejs-example/ vanduc@VGN-FZ290E:~/test2/nodejs-example$ git checkout -f 3-basic-auth vanduc@VGN-FZ290E:~/test2/nodejs-example$ npm install vanduc@VGN-FZ290E:~/test2/nodejs-example$ node index.js
For one who already checked out my project.
vanduc@VGN-FZ290E:~/test2/nodejs-example$ git pull vanduc@VGN-FZ290E:~/test2/nodejs-example$ git checkout -f 3-basic-auth vanduc@VGN-FZ290E:~/test2/nodejs-example$ npm install vanduc@VGN-FZ290E:~/test2/nodejs-example$ node index.js
You can login with username and password is admin:123456
The code require authentication on server side:
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var app = express();
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({
secret: '1234567890QWERTY',
saveUninitialized: true,
resave: true}));
var realm = 'localhost';
function unauthorized(res, realm) {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"');
res.setHeader('Content-Type', 'text/html');
res.end("This request requires HTTP authentication");
}
function checkLogin(username, password, callback){
if(username =='admin' && password =='123456')
{
callback(null,username);
}
else
{
callback('worng user name',username);
}
};
app.use(function (req, res, next) {
var authorization = req.headers.authorization;
var session = req.session;
if (req.user) {
console.log('User already stored in request : ' + req.user);
return next();
}
if (!authorization) {
return unauthorized(res, realm);
}
var parts = authorization.split(' ');
if (parts.length !== 2) return next(error(400));
var scheme = parts[0]
, credentials = new Buffer(parts[1], 'base64').toString()
, index = credentials.indexOf(':');
if ('Basic' != scheme || index < 0)
{
return next(error(400));
}
var user = credentials.slice(0, index)
, pass = credentials.slice(index + 1);
if(session.user_id != ''&&session.user_id == user){
return next();
}
//async check authentication
checkLogin(user, pass, function (err, username) {
if (err != null) {
session.destroy();
req.user = req.remoteUser = null;
return unauthorized(res, realm);
}
req.user = req.remoteUser = user;
session.user_id = user;
next();
})
});
Then we use the tricky code for doing the logout basic authentication:
$(document).ready(function(){
$('span.logout').on('click',function(e){
e.preventDefault();
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
// code for IE
else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (window.ActiveXObject) {
// IE clear HTTP Authentication
document.execCommand("ClearAuthenticationCache");
window.location.reload();
} else {
xmlHttp.open("GET", window.location, true, "logout", "logout");
xmlHttp.send("");
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
window.location.reload();
}
}
}
return false;
});
});
In order to secured your site with basic access authentication, You must provide SSL connection.
Otherwise it is not good solution.
Some disvantages of basic-auth is you cant modify the login pop-up, It depends on the browser provider.
The user credentials will store on browers cache .... :( very bad
Next Post
Saturday, October 18, 2014
Restore grub2 after install windows
The problem is after you install linux and then you install a windows
For example after install Ubuntu 9.10 and then I install Windows operating system along with Ubuntu. The windows will override Master Boot Record on Hard driver , so from now when your computer boot up, you only can see Windows.
To solve the problem:
We need to have a CD version of Ubuntu 9.10. Boot our PC with live mode of Ubuntu 9.10
.Open Terminal ( Applications > Accessories > Terminal)
(We suppose that the Ubuntu already installed on partition sda3 of sda)
Reboot your computer and enjoy !
For example after install Ubuntu 9.10 and then I install Windows operating system along with Ubuntu. The windows will override Master Boot Record on Hard driver , so from now when your computer boot up, you only can see Windows.
To solve the problem:
We need to have a CD version of Ubuntu 9.10. Boot our PC with live mode of Ubuntu 9.10
.Open Terminal ( Applications > Accessories > Terminal)
(We suppose that the Ubuntu already installed on partition sda3 of sda)
sudo -i (change user to root user).
mount /dev/sda3 /mnt
grub-install --root-directory=/mnt/ /dev/sda
mount --bind /proc /mnt/proc
mount --bind /dev /mnt/dev
mount --bind /sys /mnt/sys
chroot /mnt update-grub
umount /mnt/sys
umount /mnt/dev
umount /mnt/proc
exit
Reboot your computer and enjoy !
Setting Global Environment Variables in LINUX
The easiest way to set an environment variable in CentOS is to use export as in :
However, variables set in such a manner are transient i.e. they will disappear the moment you exit the shell. Obviously this is not helpful when setting environment variables that need to persist even when the system reboots.
In such cases, you need to set the variables within the system wide profile.
In CentOS (I’m using v5.2), the folder /etc/profile.d/ is the recommended place to add customizations to the system profile.
For example, when installing the Sun JDK, you might need to set the JAVA_HOME and JRE_HOME environment variables. In this case:
Create a new file called java.sh
Within this file, initialize the necessary environment variables
Now when you restart your machine, the environment variables within java.sh will be automatically initialized (checkout /etc/profile if you are curious how the files in /etc/profile.d/ are loaded) .
If you want to load the environment variables within java.sh without having to restart the machine, you can use the source command as in:
Test command:
P/S : some common commands use to setup java environment
vanduc@VGN-FZ290E:~/Downloads$export JAVA_HOME=/usr/lib/java/jdk1.7.0_71
vanduc@VGN-FZ290E:~/Downloads$export PATH=$PATH:$JAVA_HOME
However, variables set in such a manner are transient i.e. they will disappear the moment you exit the shell. Obviously this is not helpful when setting environment variables that need to persist even when the system reboots.
In such cases, you need to set the variables within the system wide profile.
In CentOS (I’m using v5.2), the folder /etc/profile.d/ is the recommended place to add customizations to the system profile.
For example, when installing the Sun JDK, you might need to set the JAVA_HOME and JRE_HOME environment variables. In this case:
Create a new file called java.sh
vanduc@VGN-FZ290E:~/Downloads$ sudo vi /etc/profile.d/java.sh
Within this file, initialize the necessary environment variables
export JAVA_HOME=/usr/lib/java/jdk1.7.0_71
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JRE_HOME/bin
export PATH=$PATH:$JAVA_HOME/bin
Now when you restart your machine, the environment variables within java.sh will be automatically initialized (checkout /etc/profile if you are curious how the files in /etc/profile.d/ are loaded) .
If you want to load the environment variables within java.sh without having to restart the machine, you can use the source command as in:
vanduc@VGN-FZ290E:~/Downloads$ source /etc/profile.d/java.sh
Test command:
vanduc@VGN-FZ290E:~/Downloads$ java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) Server VM (build 24.71-b01, mixed mode)
vanduc@VGN-FZ290E:~/Downloads$
P/S : some common commands use to setup java environment
vanduc@VGN-FZ290E:~/Downloads$ cd Downloads/
vanduc@VGN-FZ290E:~/Downloads$ ls
google-chrome-stable_current_i386.deb jdk-7u71-linux-i586.tar.gz sublime-text_build-3065_i386.deb
vanduc@VGN-FZ290E:~/Downloads$ tar -zxvf jdk-7u71-linux-i586.tar.gz
vanduc@VGN-FZ290E:~/Downloads$ sudo mkdir /usr/lib/java
vanduc@VGN-FZ290E:~/Downloads$ mv jdk1.7.0_71/ /usr/lib/java/
Monday, October 13, 2014
NODEJS - CREATE STATIC SERVER WITH EXPRESS MIDDLEWARE
About NodJs
One of the great things about Node.js is that it has a built in HTTP server. This means you don't need Apache or nginx. This means serving a static site can be done in few lines of code. This article goes into how this can be achieved.
Express Static Middleware
Express has become the defacto Node.js web framework and it has great built in capabilities to serve static content. The nice thing is that not only can you serve static content you can also gzip compress it and cache it. But let's just start with the required package.json and a basic static server.
Let's start
1. Set up package.json file
Use command $npm init for creating package.json fileD:\static>npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sane defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install--save` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. name: (static) static-server version: (0.0.0) 0.0.1 description: this is an example how to create static server with nodejs entry point: (index.js) test command: git repository: keywords: author: ducnguyen license: (ISC) About to write to D:\static\package.json: { "name": "static-server", "version": "0.0.1", "description": "this is an example how to create static server with nodejs", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "ducnguyen", "license": "ISC" } Is this ok? (yes) yes D:\static>
{
"name": "static-server",
"version": "0.0.1",
"description": "this is an example how to create static server with nodejs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "ducnguyen",
"license": "ISC"
}
2. Install express module
Using command $npm install expess
Add contains for index.js file:
var express = require('express');
var app = express();
var http = require('http');
app.use(express.static(__dirname + '/public'));
var server = http.createServer(app).listen(3000, function () {
console.log('server is listenning on localhost:3000');
});
The above code is very simple, it creates an Express server, adds the static middleware and finally starts listening on port 3000 .
The static middleware handles serving up the content from a directory. In this case the 'public' directory is served up and any content (HTML, CSS, JavaScript) will be available. This means if the public directory looks like:
index.html
js/boostrap.min.js
css/boostrap.min.css
Now just start the server by type command in command line promt $node index.js, and go to bowser hit http://localhost:3000/
We can also specify the url of static server:
app.use('/static',express.static(__dirname + '/public'));
vanduc@VGN-FZ290E:~/test2$ git clone --depth=10 https://github.com/vanduc1102/nodejs-example.git vanduc@VGN-FZ290E:~/test2$ cd nodejs-example/ vanduc@VGN-FZ290E:~/test2/nodejs-example$ git checkout -f 1-static vanduc@VGN-FZ290E:~/test2/nodejs-example$ npm install vanduc@VGN-FZ290E:~/test2/nodejs-example$ node index.js
1. You will clone my repository at depth=10 means you will get last 10 commits
2. Change your directory working directory nodejs-example
3. Change the checkout to the 1-static, this was the tags with for static version.
4. Installing nodejs module need to run my application.
5. Start the application and enjoy.
Next Post
Monday, June 9, 2014
Common SQL Commands
Delete all except:
DELETE FROM edu_subscription WHERE id NOT IN (
SELECT id FROM edu_subscription
WHERE status_value_code = 'YES'
UNION
SELECT id FROM edu_subscription
WHERE status_value_code = 'NO'
)
Wednesday, January 1, 2014
Horizontal list Sencha Tounch 2.1.1
Views:
Tile-View
Ext.define('MyApp.view.TileView', {
extend: 'Ext.dataview.DataView',
alias: 'widget.mydataview',
config: {
baseCls: 'tile-data-view',
height: '100%',
id: 'tile-view',
width: '100%',
itemCls: 'tile',
store: 'ThemeStore',
scrollable: {
directionLock: true,
direction: 'horizontal',
momentumEasing: {
momentum: {
acceleration: 30,
friction: 0.5
},
bounce: {
acceleration: 0.0001,
springTension: 0.9999,
},
minVelocity: 3
},
outOfBoundRestrictFactor: 0
},
inline: {
wrap: false
},
itemTpl: [
'
',
'
{name}
',
'
{numOfCourses} courses
',
'
'
]
},
initialize: function() {
this.callParent();
var me = this.container;
//me.setWidth(2080);
var el1 = Ext.get('MainContainer');
var parent = this.getParent();
//CSBfleXcroll.defer(1, window, [this.body.dom]);
//var scroller = me.getScrollable();
//console.info('My parent is : '+parent);
//console.info('Main container : '+el1.getHeight());
//console.info('scroller : '+ scroller);
}
});
My-Panel
Ext.define('MyApp.view.MyPanel', {
extend: 'Ext.Panel',
requires: [
'MyApp.view.TileView'
],
config: {
layout: {
type: 'vbox'
},
scrollable: false,
items: [
{
xtype: 'titlebar',
docked: 'top',
itemId: 'top-bread-crumb',
items: [
{
xtype: 'button',
itemId: 'increase',
text: 'Increase'
},
{
xtype: 'button',
itemId: 'decrease',
text: 'Decrease'
}
]
},
{
xtype: 'mydataview',
itemId: 'tileview1',
flex: 1
}
],
listeners: [
{
fn: 'onIncreaseTap',
event: 'tap',
delegate: '#increase'
},
{
fn: 'onDecreaseTap',
event: 'tap',
delegate: '#decrease'
},
{
fn: 'onPanelPainted',
event: 'painted'
}
]
},
onIncreaseTap: function(button, e, eOpts) {
var tileView = this.down('#tile-view').container;
var h = tileView.getHeight();
var w = tileView.getWidth();
console.log('this get width : '+ this.getWidth());
console.log('height : '+ h + ' width : '+w);
tileView.setWidth(w+300);
},
onDecreaseTap: function(button, e, eOpts) {
var tileView = this.down('#tile-view').container;
var h = tileView.getHeight();
var w = tileView.getWidth();
console.log('this get width : '+ this.getWidth());
console.log('height : '+ h + ' width : '+w);
tileView.setWidth(w-300);
},
onPanelPainted: function(element, eOpts) {
console.log('on panel painted function');
this.containerHeight= this.element.getHeight();
this.containerWidth = this.element.getWidth();
console.log('h = '+ this.containerHeight +' w = '+ this.containerWidth);
var breadcrumb = this.down('#top-bread-crumb');
var h = breadcrumb.element.getHeight();
var w = breadcrumb.element.getWidth();
console.log('height = '+h +' widht = '+ w);
//this.setTileViewSize(h,w);
},
initialize: function() {
this.callParent();
var container = this.element;
this.containerHeight=0;
this.containerWidth=0;
console.log('container : '+container);
console.log('container width : '+ container.getWidth() + ' height : '+ container.getHeight());
var tileView = this.down('#tile-view').container;
tileView.setWidth(2080);
}
});
Store:
Ext.define('MyApp.store.ThemeStore', {
extend: 'Ext.data.Store',
requires: [
'MyApp.model.ThemeModel'
],
config: {
autoLoad: true,
model: 'MyApp.model.ThemeModel',
storeId: 'ThemeStore',
proxy: {
type: 'ajax',
url: 'data/test.json',
reader: {
type: 'json'
}
}
}
});
Model
Ext.define('MyApp.model.ThemeModel', {
extend: 'Ext.data.Model',
config: {
fields: [
{
name: 'code'
},
{
name: 'name'
},
{
name: 'numOfCourses'
}
]
}
});
Data
[
{
"code": "TH10",
"name": "Computer",
"numOfCourses": 5
},
{
"code": "TH11",
"name": "Midwifery",
"numOfCourses": 3
},
{
"code": "TH12",
"name": "Professional",
"numOfCourses": 7
},
{
"code": "TH13",
"name": "Sport and Exercise Science",
"numOfCourses": 4
},
{
"code": "TH14",
"name": "International Business",
"numOfCourses": 5
},
{
"code": "TH15",
"name": "Design Engineering",
"numOfCourses": 6
},
{
"code": "TH16",
"name": "Work Based Learning Studies",
"numOfCourses": 4
},
{
"code": "TH17",
"name": "Business Management",
"numOfCourses": 18
},
{
"code": "TH18",
"name": "Biology",
"numOfCourses": 4
},
{
"code": "TH2",
"name": "Languages",
"numOfCourses": 5
},
{
"code": "TH3",
"name": "Miscellaneous",
"numOfCourses": 36
},
{
"code": "TH5",
"name": "Financial",
"numOfCourses": 1
},
{
"code": "TH6",
"name": "Psychology",
"numOfCourses": 7
},
{
"code": "TH7",
"name": "Healthcare Science",
"numOfCourses": 5
},
{
"code": "TH8",
"name": "Criminology",
"numOfCourses": 6
},
{
"code": "TH9",
"name": "Other Course",
"numOfCourses": 6
}
]
Tuesday, December 31, 2013
Google Map API in Sencha Touch
View:
Ext.define('MyApp.view.MyPanel', {
extend: 'Ext.Panel',
config: {
layout: {
type: 'hbox'
},
items: [
{
xtype: 'list',
itemId: 'list',
width: '200px',
itemTpl: [
'
{name}
'
],
store: 'MyJsonStore'
},
{
xtype: 'map',
flex: 1,
itemId: 'googleMap',
width: 300,
mapOptions: {
disableDefaultUI: true,
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
}
],
listeners: [
{
fn: 'onListSelect',
event: 'select',
delegate: '#list'
}
]
},
onListSelect: function(dataview, record, eOpts) {
var me = this;
var googleMap = me.down('#googleMap');
console.log('name = '+ record.get('name')+' ; address = '+ record.get('address'));
if (record){
var address = record.get('address');
// Geodecode
var geocoder = new window.google.maps.Geocoder();
var map = googleMap;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == window.google.maps.GeocoderStatus.OK) {
setTimeout(function () {
map.getMap().setCenter(new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()));
var marker = new google.maps.Marker({
map: map.getMap(),
position: results[0].geometry.location
});
//map.markersArray.push(marker);
var infowindow = new google.maps.InfoWindow({
content: address,
maxWidth: 120
});
infowindow.open(map.getMap(), marker);
}, 500);
} else {
console.log('Geocode was not successful for the following reason: ' + status);
}
});
}
}
});
Store
Ext.define('MyApp.store.UniversityStore', {
extend: 'Ext.data.Store',
requires: [
'MyApp.model.University'
],
config: {
autoLoad: true,
autoSync: true,
model: 'MyApp.model.University',
storeId: 'MyJsonStore',
proxy: {
type: 'ajax',
url: 'university.json',
reader: {
type: 'json'
}
}
}
});
Model:
Ext.define('MyApp.model.University', {
extend: 'Ext.data.Model',
config: {
fields: [
{
name: 'name'
},
{
name: 'address'
}
]
}
});
Data:
[
{
"name":"New York University",
"address":"70 Washington Square S New York, NY 10012"
}
,
{
"name":"University of Zurich",
"address":"Rämistrasse 71 8006 Zürich Switzerland"
},
{
"name":"Yale University",
"address":"New Haven, CT 06520"
},
{
"name":"Hanoi University of Science and Technology",
"address":"Số 1 Đại Cồ Việt Bách Khoa, Hai Bà Trưng Hà Nội, Vietnam"
},
{
"name":"Trường Đại Học Bách Khoa Đà Nẵng",
"address":"54 Nguyễn Lương Bằng Hòa Khánh Bắc, Liên Chiểu Đà Nẵng, Vietnam"
},
{
"name":"Trường Đại học Bách khoa tp Hồ Chí Minh",
"address":"268 Lý Thường Kiệt phường 14, Quận 10 Hồ Chí Minh, Vietnam"
}
]
Add listener when click
var me = this;
var googleMap = me.down('#googleMap');
console.log('name = '+ record.get('name')+' ; address = '+ record.get('address'));
if (record){
var address = record.get('address');
// Geodecode
var geocoder = new window.google.maps.Geocoder();
var map = googleMap;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == window.google.maps.GeocoderStatus.OK) {
setTimeout(function () {
map.getMap().setCenter(new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()));
var marker = new google.maps.Marker({
map: map.getMap(),
position: results[0].geometry.location
});
//map.markersArray.push(marker);
var infowindow = new google.maps.InfoWindow({
content: address,
maxWidth: 120
});
google.maps.event.addListener(marker, 'click', (function(marker) {
return function() {
infowindow.open(map.getMap(), marker);
}
})(marker));
}, 500);
} else {
console.log('Geocode was not successful for the following reason: ' + status);
}
});
}
Subscribe to:
Posts (Atom)








