node.js - replace PHP, and asynchronously connect to it - node.js

Recently I started playing with node.js.
Since I'm developing a web app, I was interested in the benefits of google's javascript V8 engine, i started reading stuff about all this - and node.js.
An example of a webapp which uses node.js: http://bodesigns.com/simple-web-app-using-node-js/
As you can see, it uses node.js to connect to a mysql database. Some questions about this:
- First of all, is this safe? I mean: the username and password are stored in the file. I know, in PHP it is to, but a PHP-file is server-side. Node.js is server-side?
Second, with this (i'm also building a part of the web app with google maps stuff), i could replace the PHP-code I have now to collect some data from a mysql database? What are the (dis)advantages of replacing the PHP-code with node.js code?
Last: can I run a node.js server asynchronously? I mean: i have a HTML-page with a link (). When i click it, it must "run" the node.js-script, which collects data form the mysql-database. Like in the example. So when the page loads, only an empty map has to be visible. If you click something, markers have to show or hide. Is this possible?
My apologies for my bad English.

First of all, is this save?
node.js runs on the server, the code doesn't need to go to the client so it's safe.
What are the (dis)advantages of replacing the PHP-code with node.js code?
Do you need to rewrite code that already works? If your writing new code then PHP vs node.js is a completely different and application specific discussion. For generic applications they are both suitable
Is this possible?
Yes, however it's also possible with PHP.

Related

AngularJS with Express Templates or pure HTML? Pros and Cons?

Express JS uses templates for generating HTML and then server sends them to client in response. There may be several other templates from which HTML can be generated. The ones I was able to discover are:
Jade (http://jade-lang.com/)
EJS (http://embeddedjs.com/)
In my app, I need to use both ExpressJS and AngularJs. I am new to both technologies. While learning angular, I had to use it in pure HTML. After learning ExpressJs, I realized, in order to use angularjs, I need to use them in any of the above templates which will be converted to HTML while sending to client.
Now, I want to use expressjs as my server and angularjs as my client side app. For this, I think I have two options.
Option 1
I can stop using templates altogether and use our NodeJS server to respond by sending simple HTML files. These HTML files will then contain AngularJS coding within them. AngularJS then, on client side, will act as our application. It will demand other HTML documents from the server. Or it can also be used like AJAX, where we can only request the piece of information to update just part of the page rather refreshing the whole page for a minor change.
Option 2
I can use angularjs inside expressjs templates (jade or ejs).
Kindly, help me in understanding the pros and cons of both options. Which one will be your choice in such case.
This is very much an opinion question and Stack Overflow admins hate anything that smacks of opinion, but here's my experience and opinion nevertheless.
I've done a couple of apps now using purely static files (HTML, CSS, and JavaScript) with those calling a service on the back-end to deliver the data. It reduces the back-end, whatever it is (I've used both Java and Node.js), to just being a set of service URLs but it works very very well.
You've got a fantastic hard line between the responsibilities of the
two systems
It's very easy to work on and test each one independently
Bugs are usually very clearly in the front-end or the back-end (all
you have to do is look at the data transferred to know)
The back-end services are ready to be reused to support alternative UIs from the
command line or something mobile specific if you want
You can use one technology for the back-end to start with (say Node.js or Ruby on Rails) and then switch to something else later if you need to. As long as the API stays the same the front-end never knows.
I personally use AngularJS with Express/Jade. The setup is actually pretty simple and I find writing Jade much more enjoyable than writing HTML. I've also adopted writing my Angular code in CoffeeScript as, again, it makes for quicker development. If you are looking to save keystrokes then Jade is a great solution and its integration with Express makes it a no brainer. If you aren't worried about producing code more quickly then there is absolutely no problem with using HTML.
I will point out that one of the greatest benefits I have found to using Jade over HTML is the ability to develop a single page in multiple files, then use include to have them concated before compiling into HTML. This allows you to take larger pages and break them into more manageable chunks. Together with Angular's templating, this can relieve much frustration.
Really it is all a matter of opinion, but since I decided to give Jade a shot, I have not regretted it and I have never ran into a situation where my HTML was rendered incorrectly when using Angular.
I went with option 1 because I didn't want to deal with any potential issues with jade or ejs converting the template incorrectly and interfering with Angular. My app essentially has the index page (which is really just the basic page template with my css and js includes) come out of Express as jade and then angular takes it come there and all my angular templates are in a separate location than my jade template.

Using ember.js with node.js + express.js

After years of PHP/MySQL development on the server-side, I'm trying to explore new technologies for building modern web applications.
I'm trying to make an order between all the JavaScript stuff and, if I have understood well, a valid solution can be node.js on the server-side, express.js to handle routing and, for example, ember.js and/or knockout.js to handle client-side with MVC/MVVM, binding, observers. Is it a valid architecture?
I built a pretty straightfoward website at the following github location
https://github.com/bwship/neptunejs
It uses node for the server side and ember for the clientside. It is also using Parse.com for it's data and api layer. But, the general idea should all be layed out for you there, including what I feel is a pretty solid folder and file structure.
Let me know if you have any questions. I came from an ASP.NET background, and have just completely fallen in love with client-side MVC.

Google feed api in a nodejs application?

I have a php application that uses the google feed api to get the feeds I want and display them. I was wondering how can I call the google feed api from a nodejs application ?
Use node's http.request() (or the shortcut version http.get()) functionality; that's conceptually the same thing you must be currently doing in PHP.
You might find that installing and using Mikeal's request module would make things even simpler.

Building a web site with photo uploading possibilities with node.js

I'm new into Node.js and my intention is to build a web site similar (but way less complex) than Imgur, where uploading images is possible by dragging photos from desktop to the browser.
For this I want to use Node.js and MongoDB.
I have been looking around everywhere and found a lots of tutorials (many of them out dated) for setting up a database, but none for file uploading. Node.js and all it's modules is like a jungle, and it really isn't easy to know which modules to use in which type of context.
So, what I really could use here is some help with suggestions of tutorials and/or modules that may fit for this purpose. What is the easiest and best way to get started with this?
This is a school project that I need to do (I selected Node.js for server side myself, not knowing how complex it really is and now it's to late to change), so I would really appreciate your help here.
Thanks in advance!
You can get away without using most of the node.js modules aside from the mongodb driver. Express is a popular framework for web applications, but it might even be overkill here. Really, you just need to serve some HTML with the drag and drop code, and then be able to receive and serve images. It's probably less than 50 lines of code in actual node.js, plus whatever frontend code you have.
Check out this tutorial for the image upload portion on the node.js side using express:
https://github.com/visionmedia/express/blob/master/examples/multipart/app.js
Here's a tutorial for the frontend drag and drop functionality:
http://www.thebuzzmedia.com/html5-drag-and-drop-and-file-api-tutorial/
Update You might also consider http://mongoosejs.com/, which makes mongodb interaction a bit easier--but the native driver isn't too bad to use by itself.

CoffeeScript, Node.js, MongoDB and JasperReports, is it possible?

I just realized a JSF (web JavaScript application) that connect to a MongoDB database and generate a report (PDF) thanks to JasperReports Library (so with a .java file).
Is it possible to do the same thing with a web application in CoffeeScript and Node.js? I am really new to this, so I don't know where to start.
For example: Is it possible to run a .java thanks to Node.js or CoffeeScript?
JSF is actually a JavaServer Faces application, not a JavaScript application. JavaScript (which Node.js, CoffeeScript are based on) is not equivalent to Java despite the similarity in names.
If you already have a JSF-based application then you already should have everything you need to create a web-based application. There is no need for Node.js, CoffeeScript, etc.
Go take a look at JSP, Tomcat, etc. to figure out how to render web pages rather than output to PDF and you should be set.
If you want to rewrite your application in javascript and run it on node.js, you should be able to use PDFKit to build the PDFs.
If you only wish to use your existing application and integrate it with a node.js server, I think you would be wise to run them in parallel (or proxy requests to your JSF application through node, in case that makes sense). It's hard to give a better answer unless you can state more clearly what it is you want to accomplish.
Yes, it's possible. You can start / stop / control another process with Node.js - so You can start .java in another process and use it to generate PDF. But ... it may be not very efficient in terms of resource usage, so it's an open question.
Or, you can write a simple worker that will generate PDF from JSON data and use 0MQ or other inter-process communications between Node.js and this Java worker.
He-he, JSF was one of the worst technologies I ever saw (I believe it's still in the same state now as it was in 2007 when I left Java), even dumb JSP and servlets was better...

Resources