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

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!

Related

Make a logger for Node Js

I have a project in Node Js, which executes the project on port 3000 and I access from ngrok with my browser to said localhost port, and it executes a server on port 3001 to make requests to a Maria database db. The project is done in react and the server with express.
I want to save the application logs (errors, warnings, etc.) in a log file so that I can see them whenever I want.
My intention was to use winston, and while I have no problem on the server side (3001), when I try to adapt it to the main project, I get an error that it cannot save files (the reason that appears is that it runs from the browser, and you can't create such a file because you don't have access to the project folders)
Can anyone give me some advice? Am I wrong to use winston, and should I use another?
Greetings and thanks
I've never used winston before and I couldn't find anything online about your error. In the past I've always just used node's fs module to create a log of errors and restarts.
const fs = require('fs')
Node's File System Documentation: https://nodejs.dev/learn/the-nodejs-fs-module
Short YouTube Tutorial: https://www.youtube.com/watch?v=U57kU311-nE

How to get nodejs on client-side on cordova?

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.

Web-based MongoDB (Mongo-express) how to use middleware

I'm new at nodejs and I gues therefor I can't get to it.
I'm trying to install https://github.com/mongo-express/mongo-express as middleware (I got it to work as standalone app).
I did those three steps
var mongo_express = require('mongo-express/lib/middleware')
var mongo_express_config = require('./mongo_express_config')
app.use('/mongo_express', mongo_express(mongo_express_config))
And after that not sure how to reach it(how do I run it). I hoped for the port 8081 to be active but it's not.
Can anyone help me and explain how this supposed to work?

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

I cant seem to get imagemagick working on amazon web services

Basically I've been struggling for a while now on getting imagemagick and or graphicsmagick to run properly with my node app. So far I followed the installation source from http://www.imagemagick.org/script/install-source.php#unix
Details on my server include
nodejs
mongodb
mongoose
imagemagick
graphicsmagick
express
and it seems the installation went well. However when I run the code through node with gm, I don't get any errors but the files don't get written. I'll post an example code of the image post function.
var gm = require('./gm');
var newRoute = '/some/user/url/';
var files = req.files;
gm(files.file.path).resize(1126).compress('JPEG').quality(quality)
.write(newRoute,function(err){
//do some stuff to save changes on db
});
Now this is currently working perfectly on my local device. However on the server it wont budge. Does anyone have any ideas on what's going on?.
The app creates folders and the folders modes are supposed to be 0777, though when I log in with ssh it seems like they might be 0755', although Im not sure it's all that to do with permissions since I've got mp3 uploads working fine. It's whenimagemagickandgraphicsmagick` come in to play that this happens. Any Ideas?

Resources