MySql Workbench opens up in Safe update mode, that does not allow any updates to the tables.
To update tables, set this variable and you are good to go:
SET SQL_SAFE_UPDATES=0;
MySql Workbench opens up in Safe update mode, that does not allow any updates to the tables.
To update tables, set this variable and you are good to go:
SET SQL_SAFE_UPDATES=0;
HtmlUnit outputs a lot of logs on the console, and when my app was running for a few days straight, the log file size got huge and most of it was htmlunit log. I got rid of all the INFO logs, and all the WARNING logs were due to javascript error in the websites, so I set it to SEVERE.
In tomcat home directory, find logs/logging.properties file and add the following bits
org.apache.http.level = INFO com.gargoylesoftware.htmlunit.level = SEVERE com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter.level = OFF com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject.level = OFF com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument.level = OFF com.gargoylesoftware.htmlunit.html.HtmlScript.level = OFF
I was facing a similar problem some time back, when I was writing an android app, and wanted to test it in the emulator with the display settings of the phone.
This is how I set up android emulator for samsung galaxy s2.
Generally you will see an error like this when a npm node package is not installed properly or not installed in proper location.
Error :
node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: Cannot find module 'XYZ' at Function._resolveFilename (module.js:334:11) at Function._load (module.js:279:25) at Module.require (module.js:357:17) at require (module.js:368:17)
In my case i have seen this happening when I have installed a node module using -g switch, so to fix this I would uninstall the module again and install it without -g switch as shown below :
npm install XYZ
In this example, we will make a node application to add some data into couchdb.
http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=[YOUR-KEY]&text=holi&has_geo=true&extras=geo&per_page=1000&format=json&nojsoncallback=1
This will give you a JSON response, save it as “data.json”.
mkdir geoex cd geoex
npm install cradle
(I tried installing cradle with -g switch but I got the error as below when running the application, so i will not recommend using -g switch)
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'cradle'
function importdata()
{
var cradle = require("cradle"),
sys = require("sys"),
fs = require("fs");
var connection = new (cradle.Connection)("localhost",5984);
var db = connection.database('geoexample');
data = fs.readFileSync("data.json","utf-8");
flickr = JSON.parse(data);
for(p in flickr.photos.photo)
{
photo = flickr.photos.photo[p];
photo.geometry = {"type":"Point", "coordinates" : [photo.longitude, photo.latitude] };
// save url to flickr image
photo.image_url_small = "http://farm"+photo.farm+".static.flickr.com/"+photo.server+"/"+photo.id+"_"+photo.secret+"_s.jpg";
db.save(photo.id,photo,function(er,ok)
{
if (er) {sys.puts("error: " + er); return; }
});
}
}
exports.importdata = importdata;
var http = require("http");
var url = require("url");
var importd = require("./importflickr");
function onRequest(request, response)
{
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
if(pathname=="/import")
{
importd.importdata();
response.writeHead(200,{"Content-Type":"text/plain"});
response.write("Imported the data from flickr into couchdb");
response.end();
}
}
http.createServer(onRequest).listen(9999);
console.log("Server has started.");
node index.js
While coding one of the android app, i overwrote OnConfigurationChanged method to change the app orientation when user changes orientation. But there was a problem if the user starts the app in landscape mode, my app would by default still load in portrait mode. So add this little bit of code to check the orientation when you start your app and set the it accordingly.
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Configuration conf = this.getResources().getConfiguration();
if (conf.orientation== Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.main_landscape);
}
else if (conf.orientation== Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.main);
}
InitUI();
}
CouchDB is a database that uses JSON for documents, Javascript for MapReduce queries and HTTP for API.
CouchApp is basically a Java script and Html5 application that can be deployed into CouchDB. The app is stored in
JSON format in CouchDB and served through HTTP by CouchDB. You will want to use CouchDB if you are more of a front-
end guy who needs just html and javascript to design the site, no need for server side code, just the data from
database. So your technology stack will look like Html + JavaScript + CouchDB.
Futon is a web interface for database management in CouchDB, you can access it on your machine as http://127.0.0.1:5984/_utils.
Create a database by name portfoliodb, and add a few documents.
As you are adding these documents through the web interface you will notice two other properties are added automatically, “_id” and “_rev”. _id is the id for document and _rev is to track multiple revisions for the document.
{ “name” : “Microsoft”, “sector” : “Technology” , “count” : 10, “price” : 30 }
{ “name” : “Google” , “sector” : “Technology” , “count” : 10, “price” : 600 }
{ “name” : “Facebook” , “sector” : “Technology” , “count” : 10, “price” : 700 }
{ “name” : “Credit Suisse” , “sector” : “Banking” , “count” : 10, “price” : 25 }
{ “name” : “Goldman Sachs” , “sector” : “Banking” , “count” : 10, “price” : 113 }
couchapp generate myapp
This will create a dir structure for the app. Move into that directory.
cd myapp
Views use map reduce to return the list of documents, and this map reduce is done in javascript.
couchapp generate view techworth
This will create a directory techworth in views directory, inside this directory you will see two js, one is map and another reduce.
function(doc)
{
if (doc.sector=="Technology")
{
emit(doc.name,doc.count * doc.price);
}
}
function(keys,values,rereduce)
{
return sum(values);
}
couchapp push portfoliodb
And you will see a result like this :
{"rows":[
{"key":null,"value":13300}
]}
This tutorial is just to show how you can have javascript to pull data from database, you can access this data using myapp/techworth as JSON is your javascript in front-end. And now that we have done it, you can add a lot more on top of it to make some amazing CouchApps.
This is a short piece of code to show maps in a nodejs application.
We will need some 3rd party modules, so install these modules
npm install ejs npm install express npm install geohash
Now lets start with the code. Create a file index.js :
var express = require("express");
var app = express.createServer();
var geohash = require("geohash").GeoHash;
// route routing is very easy with express, this will handle the request for root directory contents.
// :id is used here to pattern match with the first value after the forward slash.
app.get("/:id",function (req,res)
{
//decode the geohash with geohash module
var latlon = geohash.decodeGeoHash(req.params["id"]);
console.log("latlon : " + latlon);
var lat = latlon.latitude[2];
console.log("lat : " + lat);
var lon = latlon.longitude[2];
console.log("lon : " + lon);
zoom = req.params["id"].length + 2;
console.log("zoom : " + zoom);
// now we use the templating capabilities of express and call our template to render the view, and pass a few parameters to it
res.render("index.ejs", { layout: false, lat:lat, lon:lon, zoom:zoom, geohash:req.params["id"]});
});
app.listen(9999);
Now create a folder ‘views’ in your current working directory (where you just saved index.js), and inside it create index.ejs with the code below.
<html>
<head>
<title>GeoHash</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var loadMap = function()
{
var myOptions = {
center: new google.maps.LatLng(<%=lat%>, <%=lon%>),
zoom: <%=zoom%>,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"),
myOptions);
};
window.onload= loadMap;
</script>
</head>
<body>
<div id="map" style="width:500px;height:500px;"></div>
</body>
</html>
Here we define a loadMap function that will be called on window load, all we do is create a map instance with google.map.Map and give it the options passed as parameter passed from index.js.
With all this you are ready to go. Start your node application :
node index.js
Now as this is running on your localmachine, try this url : http://localhost:9999/gcpvj0fb970t1
As you will have noticed by now “gcpvj0fb970t1″, is a geohash for a location, try the code to find what location it is!
Cheers
In this post we will build up on the simple nodejs application we wrote earlier and add the functionality to allow user to upload a pdf file and show the pdf back to the user.
I will not be describe the old code as we have already read a lot of documentation on it in previous post.
So lets start coding and there will be comments for new code in between :
Index.js :
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandler");
var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;
server.start(router.route,handle);
Server.js
var http = require("http");
var url = require("url");
function start(route,handle)
{
function onRequest(request, response)
{
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(handle,pathname, response, request);
}
http.createServer(onRequest).listen(9999);
console.log("Server has started.");
}
exports.start = start;
Router.js
function route(handle,pathname,response,request)
{
console.log("About to route a request for " + pathname);
if (typeof handle[pathname]==='function')
{
handle[pathname](response,request);
}
else
{
console.log("No request handler found for " + pathname);
response.writeHead(404,{"Content-Type":"text/plain"});
response.write("404 Not found");
response.end();
}
}
exports.route = route;
Finally requestHandler.js, this is the file where we make change to allow uploading a file, and then displaying it back in the browser.
Before this, you will need to install a package called formidable, this package makes it easy to upload and deal with all the file processing relating to upload. Install it using this command :
npm install formidable
requestHandler.js contents :
var querystring = require("querystring");
var formidable = require("formidable");
var fs = require ("fs");
function start(response)
{
console.log("Request handler start was called.");
var body = "<html>" +
"<head>" +
"<meta http-equiv='Content-Type' content='text/html'; " +
"charset='UTF-8' />" +
"</head>" +
"<body>" +
"<form action='/upload' enctype='multipart/form-data' method='post'>" +
"<input type='file' name='upload'></input>" +
"<input type='submit' value='Upload file' />" +
"</form>" +
"</body>" +
"</html>" ;
response.writeHead(200,{"Content-Type":"text/html"});
response.write(body);
response.end();
}
function upload(response,request)
{
console.log("Request handler upload was called.");
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error,fields,files)
{
console.log("parsing done");
/* possible error on windows systems :
tried to rename to an already existing file */
fs.rename(files.upload.path,"c:\\tmp\\test.pdf", function (err)
{
if (err)
{
fs.unlink("c:\\tmp\\test.pdf");
console.log("Value of files.upload.path : " + files.upload.path );
fs.rename(files.upload.path,"c:\\tmp\\test.pdf");
}
});
response.writeHead(200, {"Content-Type" : "text/html" } );
response.write("Received pdf: <br/>");
response.write("<a href="/show">/show</a>");
response.end();
});
}
function show(response, request)
{
console.log("Request handler show was called.");
fs.readFile("c:\\tmp\\test.pdf","binary", function(error,file)
{
if (error)
{
response.writeHead(500, {"Content-Type": "text/plain" });
response.write(error + "\n");
response.end();
}
else
{
response.writeHead(200, {"Content-Type" : "application/pdf" });
response.write(file, "binary" );
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;
And then run the app :
node index.js
You will be able to upload the file and see the pdf back as embedded in page in the browser.
A simple example of how to write a nodejs application. (This is from the nodejs for beginner ebook).
What is NodeJs? – Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications.
Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for
data-intensive real-time applications that run across distributed devices.
For this example we can have all functions in same file / module but for sake of clarity we will create functions in different modules.
The js files mentioned below will need to be in the same directory. (You can keep it in different directory given that you also update the file path reference accordingly).
Create Index.js :
// create instance of server module that we have defined in another js file
var server = require("./server");
// create instance of router module that we have defined in another js file
var router = require("./router");
// create instance of requestHandler module that we have defined in another js file
var requestHandlers = require("./requestHandler");
// here we use an array to create a mapping of url pattern and the functions that will handle processing for those requests.
var handle = {}
// start function will need to be exported by requestHandler module
handle["/"] = requestHandlers.start;
// start function will need to be exported by requestHandler module
handle["/start"] = requestHandlers.start;
// upload function will need to be exported by requestHandler module
handle["/upload"] = requestHandlers.upload;
// the server module created above will need to export a start function with two arguments, and we call that for further processing
server.start(router.route,handle);
Here we are creating instance of server, router, requestHandler module that are defined by us in separate js files.
We will also associate url mapping with different modules so we can separate processing of different request in different module and have clearner js files
.
Now we define server.js file like this :
// create instance of http module of nodejs
var http = require("http");
// create instance of url module of nodejs
var url = require("url");
// define a function start
function start(route,handle)
{
// we will use this function to process incoming requests from http server
function onRequest(request, response)
{
// extract the pathname from the url requested by user
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
// invoke route with the url-handler pair mappings, the pathname requested by user, and response object where output can be sent for the user.
// route function is responsible to routing the request based on pathname to the appropriate handler
route(handle,pathname, response);
}
// we invoke createServer function exposed by http module and listen on port 9999
// here onRequest function is called when we receive a request.
http.createServer(onRequest).listen(9999);
console.log("Server has started.");
}
// we export the start function above as start
exports.start = start;
Here we create instances of nodejs modules : http and url.
http module provides us with function to create an http server listening at a particular port.
url module provides us with functions to perform processing on urls (ofcourse), like getting path, querystring etc.
Now lets define router.js file :
// we define a route function that takes url-handler pair mappings, pathname from user url request, and response object for user
function route(handle,pathname,response)
{
console.log("About to route a request for " + pathname);
// check if we have a handler for this pathname pattern
if (typeof handle[pathname]==='function')
{
// invoke the handler
handle[pathname](response);
}
else
{
// else we send a 404 response
console.log("No request handler found for " + pathname);
response.writeHead(404,{"Content-Type":"text/plain"});
response.write("404 Not found");
response.end();
}
}
// we export the route function above as route
exports.route = route;
Finally the requestHandler.js contains the logic to perform appropriate actions for a requested pathname :
// create instance of child process module to use the exec function asynchronously
var exec = require("child_process").exec;
function start(response)
{
console.log("Request handler 'start' was called.");
// we invoke dir system command, you can replace it with "ls -l" if you are using linux
// only when the command has finished the callback function in second argument is called with the three parameters having values from corresponding streams
// we write the output from stdout back to the user and close the response object
exec("dir",function(error, stdout,stderr)
{
response.writeHead(200,{"Content-Type":"text/plain"});
response.write(stdout);
response.end();
});
}
// a placeholder function that just write a Hello message back to user
function upload(response)
{
console.log("Request handler 'upload' was called.");
response.writeHead(200,{"Content-Type":"text/plain"});
response.write("Hello Upload");
response.end();
}
// exports start function
exports.start = start;
// exports upload function
exports.upload = upload
Once you have done all of this you can start your application like this :
node index.js
Happy Nodejs’ing!