Saturday, February 28, 2015

Create a simple server using Node and Express


Recently I got selected to build the website of the Techno-Cult Festival, Advaita, of my college. My role was to develop backend and handle database of the website. I was new to backend development. I was thinking to use PHP or Python which are suitable for this task. Then why Node? Answer is simple, Node is very simple to learn and the best thing is, its Javascript (Universal language for web development). Community is very dynamic plus lots of extensions and modules are developed daily. You can get modules available to do alomst anything using node.

Basic Node Server
Lets see a basic example, Node server without any module.

var http = require("http");
http.createServer(function (request, response) {
    response.writeHead(200, {
        'Content-Type': 'text/plain'
    });
    response.write('This is a response for a request')
    response.end();
}).listen(8100);

Node is javascript but on the server side... :-)
This code implements a simple web server which responds to get requests with a message. The require statement is a feature of Node which helps to add modules to the app.


Node Server with Express Module
We just got to know how simple it is to build a server. But, there are many modules available that provide an extra bit of abstraction to the app and help easy to implement something. Like passport, Express, Express router etc. They just decrease our work as well as lines of code. So, we are gonna use one such module, Express. Why Express? Because it provides:
  • Routing Mechanisms using express-router
  • Support for view engines like Jade, Ejs
  • Basic Authentication
  • Cookies manipulation
  • many more

var express = require("express");
var app = express();
 
app.get("/", function(req, res) {
    res.sendfile('index.html')
});
 
app.post("/login", function(req, res) { 
    //Some server side logic to handle this
    res.send("OK");
});
 
app.get(/fileDownload, function(req, res){ 
    res.sendfile( __dirname + "file.txt"); 
});
 
var port = process.env.PORT || 8100;
app.listen(port, function() {
    console.log("Listening on " + port);
});

The first thing we have done is to import the express module using the require statement and then we use this modules object (app) to define get, post methods, routing and every task we need to implement using express. 
The syntax is too easy. We have to pass the link which matches the route and a function which is called when the link is matched. The function gives us access to the request and response wrappers. 



Reference: ExpressJS

Saturday, September 20, 2014

Making a window draggable in Java

In Java generally a frame has a border which gives the window the drag functionality. So, in custom windows, if we remove the border, we will lose the drag function.

We can enable that drag functionality using the MouseListener and MouseMotionListener interfaces of Java API. Following is and example code depicting the usage where NewJFrame is the class of the Java Frame:


class MoveMouseListener implements MouseListener, MouseMotionListener {
 NewJFrame target;
 Point start_drag;
 Point start_loc;

 public MoveMouseListener(NewJFrame target) {
  this.target = target;
 }

 public static JFrame getFrame(Container target) {
  if (target instanceof JFrame) {
    return (JFrame) target;
  }
  return getFrame(target.getParent());
 }

 Point getScreenLocation(MouseEvent e) {
  Point cursor = e.getPoint();
  Point target_location = this.target.getLocationOnScreen();
  return new Point((int) (target_location.getX() + cursor.getX()),
   (int) (target_location.getY() + cursor.getY()));
 }

 public void mouseClicked(MouseEvent e) {
 }

 public void mouseEntered(MouseEvent e) {
 }

 public void mouseExited(MouseEvent e) {
 }

 public void mousePressed(MouseEvent e) {
  this.start_drag = this.getScreenLocation(e);
  this.start_loc = this.getFrame(this.target).getLocation();
 }

 public void mouseReleased(MouseEvent e) {
 }

 public void mouseDragged(MouseEvent e) {
  Point current = this.getScreenLocation(e);
  Point offset = new Point((int) current.getX() - (int) start_drag.getX(),
   (int) current.getY() - (int) start_drag.getY());
  JFrame frame = this.getFrame(target);
  Point new_location = new Point(
   (int) (this.start_loc.getX() + offset.getX()), (int) (this.start_loc
    .getY() + offset.getY()));
  frame.setLocation(new_location);
 }

 public void mouseMoved(MouseEvent e) {
 }
}

Sunday, June 8, 2014

Gtk3.0 for Windows : Learning to contribute

Contacted the community about how to start to contribute to GTK+ for windows and in the rply, I was asked to compile the GTK+ bundle. Probably upto Gtk+3.6.4, there were no important glitches so, I compiled that easily. Link to get the archive is : http://www.tarnyko.net/repo/gtk3_build_system/

There's a readme that tells how to do that. I got that easily and reported back about my work. 
One thing to note is that, its missed to mention in readme that we have to copy all the folders, except bin, of archive to our new folder, for my case: C:\gtk .

Then, I proceded to successing versions i.e 3.8 and 3.10. The 3.8 version compiled fine. But 3.10 has glitches. 

When I ran the first command i.e pkg-config, I got the following error: 


C:\gtk10>pkg-config --cflags gtk+-3.0
Package gtk+-3.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-3.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk+-3.0' found


It occurs when the bin folder is not added to the path (but, I had already added). So, there's a bug here (I think). 

Tried to find the source code, but was unable to find. So have asked community for help to get the source.