How to convert node.js application into cordova - javascript

we built a node.js application for desktop and tablet. It's completely web based application. Now I am planning to implement same application as native app in android using Apache cordova.
Under project directory, we have node_modules,public,.... all client side files are in public folder. When I invoke URL in browser, from client side I am making API call to check whether user already logged or not. like this we are making API calls to my server.
As per my understanding,native app is nothing but we are storing all the client side files into device. whenever user open app will load client side files and as per work flow it will make API calls.
Theoretically I understand that much.
where I stuck :
In desktop app , I used to make API calls with URL like /api/web/shared/reject/, here we don't need to mention server address like localhost:8080/api/web/shared/reject/ that everything browser will take care. This same thing how can I make it work in cordova applications.
How cordova will know whether it is localhost or something else...
Regarding this, I Goggled but I didn't find any tutorials.
can anyone suggest me the way.

Basically, is a concept problem.
Node.js is a technology specialised in backend and some of usages like extend some services/functions, etc.
When you talk of a localhost:8080 you are talking that your node.js implementation should be in a server (Amazon, Azure, your own server, nodejitsu, etc), and the public pages or the pages that the client should consume will be added into phonegap, specifically in your www directory and the references for localhost:8080 should be changed for your server (Amazon, your own server, nodejitsu, etc), and the files on your www directory could have references (via GET or POST to retrieve the data from the server. Remember, the Crossdomain problem doesn't happens on phonegap (maybe in a local enviroment should occur).
And Phonegap is a framework to develop front-end with HTML5, jquery, CSS3 and other releated technologies.
For your specific case the node_modules should be installed in the server too, not on the phonegap project.

Related

How to use a Node.js web server with an Apache Cordova Application

I have designed an Apache Cordova Application that uses a Node.js web server to pull data from a web API and allows the Javascript based project to use data pulled from this API.
Is there a way that I can use this Node web server across an iphone implementation of this cordova application? I am currently testing the application by pulling data from the website's API, hosting in on a node (ex. localhost:3000) and then pulling the data from that address in the JS file in my project.
How can I implement this server so that it can be used on an iOS mobile device?
If you want to host your Node server somewhere so that devices can see it on a network, then Amazon AWS Elastic Beanstalk has an easy solution for that - just upload your Node project in a Zip file and they manage the server environment for you.
There's several other options out there depending on your needs, for example Heroku may be a good alternative.

socket.io.js 404 (Using apache to host site)

Alright, so I had my socket.io server listening on a different port, but in order to get it to work with https, I needed to have it listen without passing in a port (default). (It works fine on a different port loaded with http, but I need it to work on https)
My project was working fine, client could connect and send data fine. However, I moved the site over to my main domain, which has an SSL certificate. The site loads everything via https, so it couldn't load the http version of socket.io.js
However, now that I switched it to just var client = require("socket.io").listen().sockets; instead of listening on a different/specific port , it's still not working. Instead of giving me a connection error, it's not including the file at all.
My fear is that I'd end up needing to remake my whole site to host my files via node.js and I'd rather not have to do that.
I'm not using any other module than mysql-node and socket.io, and I'd prefer to keep it that way if possible. I am new to node.js, so I'm sorry if there's an obvious answer that I'm unaware of.
I looked around, however, and can't seem to find the answer anywhere. Or, at least a clear answer.
Would I be better off using websockets instead of socket.io?
If so, how would I go about doing this? I'd be more willing to remake my node application instead of remaking my site, honestly.
I am including the socket.io.js file in the client-side like so:
<script src="https://mysite/socket-io/socket.io.js"></script>
but of course, 404 since it's not an actual file that's on my apache server. There's no folder/directory named socket-io in my public_html directory, so that makes sense to me.
But, how can I get this to work? Would I need to host my files via node.js or would I be better off using HTML5 websockets? A fairly large demographic of my site's users use mobile devices, so I'd have to be sure it works on mobile as well.
If you're going to use apache to host the socket.io.js file, then you need to put that file on your Apache server at a path that it can be served from by Apache, just like any other web file that you want the Apache server to serve. Or, you can just serve socket.io.js from a public CDN also and use the public CDN URL. It's just a JS file. You can put it anywhere or use any URL that reaches a place where the file will be served from. There are some advantages to letting node.js and socket.io serve it for you because it guarantees that client and server socket.io versions are always in sync, but you don't have to do it that way.
If you are using node.js (which it sounds like you are at least in some capacity), then the socket.io built into node.js will serve the file automatically if you are using node.js to serve your web page too and you've configured socket.io to listen on the same port as your node.js web server. In that case, your webpage and socket.io will use the same port and both will run through the node.js server.
You haven't really explained why you're using both node.js and Apache, how that architecture works and why you're serving some of your site with Apache rather than just using node.js for the whole site as that is certainly the cleaner option with socket.io.
You can use plain webSockets if you want instead of socket.io, but then you will likely have to build some of the socket.io functionality on top of the webSockets (auto-reconnect, message passing, etc...) and using plain webSockets won't really simplify any of the Apache/node.js questions you have. It's trivial to serve up the socket.io.js file to the client using either Apache or node.js and once the client has the file, it is actually more work to use plain webSockets than to use socket.io because of the extra features that socket.io has already built.

How to access database in an HTML5/JS app online and offline with one code base?

I'm working on an HTML/JavaScript GUI application that will run on mobile devices (using Cordova) and also as a hosted web page that's accessible from a desktop web browser. I want to give the option to read and store data in a local offline database (SQL database most likely), as well as the option to connect to a web server and read/update data from the server as well.
If anyone has done something similar without having to write the data access routines twice (once for the server side, and once for the client offline storage side), I'd like to get some suggestions.
One solution I am thinking about (which has some unresolved issues still):
I could write the server in any platform (PHP, Java, Js, etc.), but don't want to replicate the data access code for the offline version, so am thinking to do the data access portion in JavaScript--maybe write a node.js server, and use sqlite for local/offline databases (which Cordova supports). I can't figure out how to provide similar local data functionality on a web browser.
The simplest option would be to run a server on the local machine, but I don't think that is easy in Cordova or on a desktop browser.
Check out PouchDB. PouchDB is compatible with CouchDB and is 100% Javascript. You can do some cool offline syncing to online syncing with CouchDB.
Check out https://cloudant.com/blog/pouchdb/ and http://pouchdb.com/faq.html.

Node.js + Node-Webkit + Node-SerialPort Based Application - Is this Possible?

I am new to Node.js and before I go head first into taking on a new technology and migrating my c# based application, I wanted to make sure what I had in mind is possible with Node.js and if it is recommended.
So please let me know your opinion!
My application has the following requirements:-
TCP server (to receive packets from TCP clients such as smart phones, computers etc.)
Serial port access (To control a hardware device)
Web server facilities (to serve HTML5 pages or provide web services intefaces)
Simple native app like GUI for configuration.
All of this needs to be packaged nicely for end users to install simply.
Why I would want to do this you may ask?? The reason I am most interested in using Node.js is due to the cross platform nature, including the ability to install on cheap single board/embedded computers.
This is my thinking:-
Node.js - to provide the TCP server, serve HTML5 pages and provide web services interfaces.
Node-Webkit - to provide the simple native app like configuration interface. I also believe that it provides the ability to package my application for simple distribution, but I am not sure? (I could leave out Node-Webkit if there was another way to package my app for simple distribution. Although it is preferred as even having to find the IP Address of the computer so as to access a web browser interface would be tricky for some of my users.)
Node-SerialPort - to provide the ability to communicate with the hardware device. The instructions will come from 1 of three sources. 1) TCP connection 2) HTML5 Webpage Initiated 3) Web Services Initiated.
That's what most people use Node.js for
https://npmjs.org/search?q=serial+port
Again, that's what most people use Node.js for
Because of 1 and 3, the most obvious UI for node.js apps is the browser. Write a config page to control your app. Using node-webkit is overkill, just use whatever browser is already available. Node can run multiple listeners on multiple ports, serving the same app.
npm install is pretty simple.
Go learn how to write simple Node.js apps (with express for easy http servering, and jade or nunjucks for easy html templating) and then move up from there.
Point 5. I understand the fact of having an installer. I would suggest that the .msi or .exe will create a Windows Service for NodeJs. Then the node js server will launch at Windows startup and the end user will access the browser with
http://localhost:< a port number>/.
Packaging NodeJS : To package nodejs, you can copy the nodejs.exe from the installation directory of nodejs into the directory where you developp the node application. Then zip everything. You unzip it on another computer and it will execute.

How to efficiently develop client-side code for a web app without installing a local backend?

One of my team members is working only on client-side (Javascript) development for a web app with a large and complex backend.
I would like to avoid the need for him install and configure a local copy of the backend.
However, I wouldn't want him to need to push every small change to the dev server just so that he can test it.
We thought about getting the client to make the requests directly to the dev server, instead of to the same domain (the localhost) but this doesn't seem practical due to cross-domain request policies and authentication problems (cookies aren't getting sent).
What are some elegant solutions for developing clients without having a local backend?
Depending on how complicated your backend is, you might be able to create a mock backend using a lightweight web framework like Sinatra. I've had some success with this technique, but the services I've been mocking have been fairly simple. In some cases the mock backend mostly serves static JSON files.
I use Charles Proxy to map the URIs of the dev server's web services to localhost (where I run a light weight web server that serves up my static development code).

Categories

Resources