Sharing a class between Node.js and the browser - node.js

If i have a class, what is the best way to share that class and use it in both the browser as well as Node.js?
For example a game that uses a 2D Vector math class on both the client-side and the node.js.

NowJS is a pretty elegant way to share variables and functions between client JS and Node JS through an automatically sync'ed shared namespace.
There's also dnode, which the author comments on in response to a posting about NowJS here: http://news.ycombinator.com/item?id=2316079

Also, Haxe, a language that lets you write using the same language for the browser and the server.

Take a look at NowJS, which should do what you want.

Write it as CommonJS/Node.js module, so it works normally in Node.js, and then pack written module for browser using Webmake. It's simplest way to share same codebase on both sides

Related

React JS app: How do I load new TTS voices?

I’m building an app with the React JS library, using Electron and Node JS to package it as a Windows .exe
I used the “speak-tts” module to integrate speech synthesis. Everything seems to be pretty straightforward, but I can’t figure out how to load additional voices. I can only get the default voice.
Has anyone else come across this issue? Your advice is much appreciated.
I'm the creator of the speak-tts module. As you can see in the documentation (https://www.npmjs.com/package/speak-tts) there is a setVoice() method that you can call on your instance to change the voice on the fly.
You can get the list of available voices in the browser from the onvoiceschanged listener. Also there might be specific issues with Electron and this package that I'm not aware of.
If so do not hesitate to create a github issue (https://github.com/tom-s/speak-tts/issues).
Easiest way to do is use plain JS methods
For reference.
https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis/cancel
It has all the required methods. cancel(), start(), pause() etc

Using nodejs in the browser?

Following my first question, I realized that the reason I couldn't get anything to work was that I was trying to use the require() method in the browser. In learning that this is not how Node works, I was confused. I understand now that Node is only a run-time environment that allows the use of JavaScript within the console. However, I'm still left with the question of how to achieve the functions of Node within the browser. (i.e. creating a button in the browser that creates a file using Node.) I've installed Browserify but I cannot find a good tutorial on how to use it. I'm very new to Computer Science so please forgive me for the redundant questions. Thanks a lot.
Things do change. Apparently it will be possible to use node.js in the browser soon. You might be interested in this: Run Node.js in the browser
Node as you made the point is server-side runtime environment for js and the browser is client-side. Node.js has many useful built-in functionalities and they are not accessible in browser.
I recommend you to take a look how backend and frontend differs and how do they communicate with themselves. Then I think you will understand how to achieve creating a file by clicking a button in the browser.
Sorry I don't know anything about browserify but it would be better to learn node in cli.

Basic use of server side API and passing server side variable to client side

I've just started my IT degree and I'm a beginner to the use of APIs (and forums like this) so I am truly sorry if my question is to vaguely explained or if it is just plain stupid :), on top of that I'm not a native English speaker :P. Okay, so I'm trying to use Google trends' api which I installed in my server with putty by using sudo npm install google-trends-api. (it can be found here https://www.npmjs.com/package/google-trends-api#installation) As I undestand it, this is a server side api so the scripts that I write with the methods provided for this api will not run on an explorer as normal js files do. There is an example that makes use of the API that I found on the page which is as follows
var googleTrends = requite('google-trends-api');
googleTrends.hotTrends('US')
.then(function(results){
console.log(results);
})
.catch(function(err){
console.log(err);
});
this outputs a list of 20 items on the console when I use it on node.
I would like to know if there is a way to assign those results to a variable and then use that variable in a normal javascript script inside a html file. I do not know anything about node.js and the like, and I would like to actually do some research instead of asking here but I was going to use a different approach to acquire such information but now I've had to change my plans and do not have enough time and given I consider this is a fairly easy problem to resolve (maybe?) I would really appreciate it if someone could walk me through the basics of each step. THanks :) and have a nice day.
Your question is quite broad. Node.js is Chrome's V8 engine bundled with some libraries to do I/O and networking. This enables us to JavaScript outside of the browser and to create backend services or servers in general (in your case). I hope that you are aware of this difference :)
The first thing that you have to do, is to have a look at express.js and to create a simple server. It will not be more than 20 lines of code. Then you have to enrich this with more stuff like a template engine (handlebars.js, jade etc). You have to enable the server to serve static files that will be finally your js, css and image files. Creating this simple server you will be able to serve simple html page in the first place. On top of that you should have the client side javascript that you have to write and now you can use the module above. Unfortunately, you are not able to use this module directly on a javascript file that you will write. To be able to use this module you have to transcompile this thing into javascript that browser understand*. Remember that browser does not understand the require statement and some old browsers possibly will have issues with the promises that this module is using. These are the things that should be compiled. You have to use a tool like browserify for this and the compiled file that this will extract it must be included in the scripts of your html page.
Maybe there are quite a lot of concepts that you are not aware of or you don't understand them but spend a bit of time to understand them.
P.S.: I' ve replied under the assumption that google-trends-api module does not use things that are specific to node.js like the file-system for example.

Yesod - shared types between server and client

I'm used to working with Dart, where sharing types between server and client is as simple as importing the relevant packages into your project.
Can something similar be accomplished with Yesod/Haskell? Should I use GHCJS for the client? Maybe Elm? The goal is not having to worry about the data getting mangled in transit between server and client - and also not having to write a single line of JS. :o)
I haven't been able to find any good, beginner friendly docs on how to best tackle this challenge using Haskell. I suspect I just haven't looked in the right places. Any and all help is more than welcome.
To achieve this with GHCJS you can just build your project out of three core packages in this fashion:
frontend - something based on ghcjs-dom, I like Reflex-dom
backend - use your favorite framework, I like Snap, Yesod should work just the same
shared - code shared between frontend and backend
Where frontend and backend both depend on shared of course. Frontend is compiled with GHCJS, backend with GHC.
If you would like to see a complete example I would highly recommend studying hsnippet. Take a look at WsApi.hs where a set of up and downstream messages is being defined. All the JSON instances are derived in one place and imported in both frontend and backend.
Hsnippet uses websockets. This is not a requirement of course. You could use regular XHR in your own app. The principle stays the same. You define your API and serialization instances (usually JSON) in the shared package and import the relevant modules in both frontend and backend.
Personally I also share validation code, database entity definitions generated with persistent etc. Once you set it up sharing additional stuff is mostly a copy paste to one of the shared modules and then import wherever.

JS library that provides simple utilities for browsers and the nodejs environment?

I'm looking for a javascript library that attempts to provide the same simple utilities in both the browser environment AND nodejs (iteration, mapping, maybe control-flow) so that code can more easily be re-used across server and client. I know you can hack out parts of any JS library (YUI, jQuery, ...) and get them to work in both environments, I'm just wondering if it's already been done or standardized.
The closest I've seen is this: https://github.com/kof/sharedjs
But it's incomplete and has some odd stuff. I'm wondering if there is something more polished before I fork and hack.
The underscore library was built to add more functional programming to jquery, things like mapping, and also templating.
Because it doesn't rely on the DOM (it leaves that to jquery) it functions well in node.
The RightJS link library has a server build link that has node.js in mind.
From the download page:
RightJS is also available as a server-side library. In this case it contains only the native JavaScript unit extensions and the Class, Observer, Options units along with all the non-DOM utility functions from the Util module.
Our server-side build follows the CommonJS principles and is ready for use with the node.js framework.
Node's GitHub wiki has a list of CommonJS-compatible modules which will run in Node and browsers.
Some of the other modules on that page may also run in a browser environment. For example, the excellent DateJS works fine in Node. (It is available as a NPM.)
Btw, RightJS is also available on NPM

Resources