Theta js. How use it with Node JS - node.js

I create a Theta Token wallet, get its balance with theta.js (#thetalabs/theta-js) in Node Js app. But when I try to get a transaction, I get error: fetch is undefined. I thing, theta.js uses fetch. How can I use theta js in Node.js apps?
I tried add node-fetch lib, but it doesn't help me.
Maybe, there is another lib for Node apps with theta?

You can use
import fetch from 'node-fetch'; globalThis.fetch = fetch;
Since you need a global scope to use fetch here.

It works for me....install isomorphic-fetch using the command:
npm i isomorphic-fetch
and then require it in your file using:
const fetch = require('isomorphic-fetch')
It will work..check this out.

Related

How can I load three/examples/jsm/loaders/GLTFLoader.js in nodejs server side

I am currently using threejs lib in nodejs server side and the following statement works well:
const THREE = require('three')
const OrbitControls = require('three-orbitcontrols')
Now, I need also to use three/examples/jsm/loaders/GLTFLoader.js but same require line does not work. It works in client side using import statement, but unfortunaly I cannot use it outside of module.
Is there a way to use this js in nodejs server side ? The reason is because I would load and build my scene in backend then pass it to the client only for rendering.
Looking forward to get some help
Yes, finally I was bale to add the jsm : import three/examples/jsm/loaders/GLTFLoader.js on nodejs

A library for nodejs connection with backend

i am creating a node application which will use a another node backend, like a CLI program, which Node.js library or framework will i use for this situation? the axios not is working in my tests.
thanks for reading.
If you want to do http requests from within node.js there are several options: https://www.npmjs.com/package/node-fetch, axios is also available. Could you please share some errors you get?
I always use node-fetch because I am most familiar with the fetch API, previously I used this package: https://www.npmjs.com/package/xmlhttprequest
As seen in your answer you are using /users as url, please use the full url. Also catch any unexpected errors.
So axios.get('http://domain/users')
I SOLVED THE PROBLEM!
the problem was in the axios connection, now its:
async function execQuery(apps){
data = ({
name: "ederson",
apps: "neofetch"
})
const response = await axios.put('http://localhost:3333/users', data
).then(console.log(apps))
}
Thanks for help me, Laurent Dhont and Ahmed Hammad!

Node.js install cloudant module

Hi I am making a Cordova app for iOS and Android. I am trying to use the cloudant module https://github.com/cloudant/nodejs-cloudant to access my cloudant couchDB, but for the life of me, I cannot figure out the issue I am having while installing it. Using linux, I executed npm install --save cloudant at the root of my Cordova project folder. When using var Cloudant = cordova.require('cloudant'); in my code, I get the error Uncaught module cloudant not found from within my cordova.js file as it tries to load the module. Also when I run $ node -e 'require("cloudant"); console.log("Cloudant works");' in my shell, I receive a terminal output stating "Cloudant works". I cannot seem to figure out what the issue is, and have tried many different things. Any help is extremely appreciated, as I am at my witts end at this point. Thanks.
As #Parth says, 'nodejs-cloudant' is a Node.js/npm module. To access Cloudant from within your Cordova application you will need client side code to interact with Cloudant's HTTP API.
You could use jQuery's Ajax functions to make HTTP requests to Cloudant or you can use PouchDB as a client-side library e.g:
<script src="pouchdb-3.4.0.min.js"></script>
<script>
var db = new PouchDB("https://myusername:mypassword#myaccount.cloudant.com/mydb";
</script>
Both solutions require you to enable CORS on your Cloudant account.
'nodejs-cloudant' is an npm module and not a cordova module. you just need to write
var Cloudant = require('cloudant'); That should solve the problem.
Try with this:
var Cloudant = require('cloudant');
Instead of importing cloudant directly, try using cradle or nano for accessing cloudant
'DB.const nano = require('nano')('//cloudant api link//');'
This works
https://www.npmjs.com/package/nano#dbinsertdoc-params-callback
https://www.npmjs.com/package/cradle

Using node.js libraries on front-end

How can I use the 'request' node.js module on the front-end?
normally I would retrieve it like so:
var request = require('request');
but this is not possible on the front-end since require is not recognized.
What is the best way to solve this?
To use node modules in the browser you can use a library called Browserify . This allows you to work with the common module pattern as well as the you can use this package browser-request to get the features of request module

Node.js + Angular = Uncaught ReferenceError: require is not defined

I'm creating an Express.js API on a Node.js server. The API is used to access data stored on the server. I also keep a log of who's accessing the API in a database.
I'm trying to create an admin section that will use Angular.js to display the admin access logs neatly. I used the Angular Express Bootstrap seed to start my project:
https://github.com/jimakker/angular-express-bootstrap-seed/
My problem is that I need the controllers.js to access node modules but it doesn't seem to know that node exists. Here is my error:
controller.js
var mongo = require('mongodb');
[Uncaught ReferenceError: require is not defined]
How can I use node modules in Angular.js files?
Node is a server side technology, you would not typically use your node modules on the browser with Angular.js. However, if you want commonjs require functionality in the browser see: https://github.com/substack/node-browserify.
Ofcourse, a browser can't talk to mongodb directly which is why you need an API in the first place, angular would communicate with your API using HTTP.
Angular.js makes an $http call to Node.js which requires and talks to the mongodb.

Resources