How to get nodejs on client-side on cordova? - node.js

I am making a app with cordova. I want the coinmarketcap stats in it. I am using nodejs and it is working fine when I execute it in my command line. However I want to use it in my cordova application. This is the link to the npm package https://www.npmjs.com/package/node-coinmarketcap
When I try to run it in a browser I get the error message require not defined. I believe that error comes because nodesj is a server-side and browser is a client side ? Am I correct ?
My code
var CoinMarketCap = require("node-coinmarketcap");
var coinmarketcap = new CoinMarketCap();
coinmarketcap.multi(coins => {
console.log(coins.getTop(10)); // Prints information about top 10 cryptocurrencies
});
I expect that I can run my code in my browser or cordova application.

You are right, Node.js is a JavaScript run-time environment that executes JavaScript code outside of a browser. You have to find some client-side JS code or execute your node code on your server and send result to your Cordova app.

Related

open webpage when start nodejs server [duplicate]

This question already has answers here:
How to use nodejs to open default browser and navigate to a specific URL
(8 answers)
Closed last month.
I was wondering if and how i can automatically open a webpage when i start my server with nodejs
app.listen(app.get("PORT"), () => {
console.log(`Servidor corriendo en http://localhost:${app.get("PORT")}`)
})
I have this basic example where when i run node start proyect it will start the server and then i manually need to click on the console logged message or open a browser and type bla bla bla
I want to raise that web for itself like when in React we run the command npm run start
I tried to google it but i dont know how to search for this because i dont get any usefull info.
Thank u very much
Use open to Open stuff like URLs, files, executables. Cross-platform.
const open = require('open');
// opens the url in the default browser
open('http://stackoverflow.com');
UPDATE
open simplifies cross platform functionality, in node you can have access to the platform by process.platform and write platform specific executions
for example here is the native version in mac without using any NPM module
const url = 'http://stackoverflow.com';
require('child_process').exec(`open ${url}`);
I don't have access to a windows system to write the code for windows

MEAN Stack Application's console.log not working?

I am currently developing a MEAN Stack application.
Previously my node version is 0.12 and I was using grunt to run my application which uses nodemon (which uses server.js) to run the server and watch to monitor for changes.
it works fine all the way till i upgrade my node to v 0.4.
now when I use grunt, the console.log does not print to console at all and it only works if i use
node server.js
instead of
grunt
the image below shows the situation that i'm facing:
in an ideal situation, when running grunt, I should be able to see the message
App listening on port 8000
however after updating node, the console.log doesn't seem to be working for my grunt(?)/nodemon(?) anymore.
can anyone advise on this matter?
thank you.
You can use very useful logging module winston. Using this you can log all your errors in a text file with timestamp, Which will be very helpful for application support. You can use it's debug, error, info methods for the corresponding console.debug(), console.error(), console.info().
EX:
Var logger = require('winston')
logger.debug('Debug things here');
logger.info('put info to track');
logger.error('to track error with status code')

Troubleshooting socket.io error message

I inherited a project which utilizes node.js and socket.io, both of which I am not too familiar with.
I installed node.js and npm via homebrew and socket.io via npm.
The current setup has two different "sites": The frontend, that the user interacts with, and the backend that handles all the communications with a DB. The application uses several different displays, and the previous developer deemed it best to use the current setup.
I can successfully start the node server by invoking node node-server.js. In the head of the node-server.js, socket.io is being called like:
var sys = require('sys')
var exec = require('child_process').exec;
var io = require('socket.io').listen(8090);
[...]
In my front end, I call socket.io like this:
<script src="http://192.168.1.111:8090/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://192.168.1.111:8090');
[...]
However, when I debug the page, I get the following error messages:
SyntaxError: Unexpected identifier 'to' (anonymous function) socket.io.js:1
ReferenceError: Can't find variable: io sample.html
In the terminal window, where I started the node server, I get the output:
debug - served static content /socket.io.js
I should add that the two sites reside in separate folders - I access the frontend through a local Apache server, the piece that interacts with the DB sits someplace else on the same machine.
How would I correctly call the socket.io in my client?
Is there a way to enable better debugging?
Update
So the to in the error message, is because whenever the browser tries to load the socket.js file, it gets the reply of Welcome to socket.io

Generate node-xmpp script for browser

I am developing an application on php. I need a chat on xmpp nodejs. Sending a message will go from web page.
I found enter link description here. In terminal everything works fine. But how do I attach client script to the browser?
I generate script by:browserify node_modules/node-xmpp/lib/node-xmpp-browserify.js > nodeXmpp.js and attach it to web page:
Then trying to use it:
$(document).ready(funcrion(){
var client = new XMPP.Client({
jid: 'user#example.com',
password: 'password'
});
});
And chrome console telling me:
Cannot load StringPrep-0.2.3 bindings (using fallback). You may need to npm install node-stringprep nodeXmpp.js:3669
event.returnValue is deprecated. Please use the standard event.preventDefault() instead. jquery.js:3254
Uncaught TypeError: Object # has no method 'resolveSrv'
Object # - its "dns".
And before generate script i install node-stringprep.
Question is how build xmpp client script for browser.
You didn't post your code, but the following should work fine:
require('node-xmpp/lib/node-xmpp-browserify.js');
var client = new XMPP.Client(opts);

now.js how to get 'now' object without browser (simple client)?

Im playing around with node.js and now.js. Everything works fine. But i would like to make a simple client that i can run from the command-line (so without the browser).
http://nowjs.com/doc/example
In the example a HTML page gets served, and that page includes the now.js file, which creates the magic 'now' object. But on a commmand-line there is no such thing.
For the server i have running (helloworld_server.js)
And the client helloworld_client.js i have:
// client.js
var nowjs = require("now");
// now i need to connect to the server (127.0.0.1:8080)
// so i i need a server object?
server = ????
var everyone = nowjs.initialize(server);
everyone.now.distributeMessage('hi!');
So how do i obtain the 'now' object?
OK, got it. Once you installed now
npm install now
it creates a node_modules folder, inside you see folders for each extension. Deeper you find:
./node_modules/now/examples
and there is the nodeclient_example folder
./node_modules/now/examples/nodeclient_example
its pretty clear from there, but those curious, this is the magic you need:
var nowjs = require('../../lib/nodeclient/now.js');
var now = nowjs.nowInitialize('http://localhost:8080');
and there it it the 'magic' now object
make sure you install :
npm install socket.io-client
otherwise it wasn't working for me!

Resources