GoogleMaps API v3 / Markers / Node.JS / Javascript / require() / exports - node.js

So, I'm developing a node.js app that will show some markers in google maps according to the locations it finds in the database, which will then be converted and saved to a .txt file. Everything works great up until that point.
If i do a console.log() of the converted latitudes and longitudes it will show them perfectly.
However, in the .html file with the maps api i have to import the file that will return the coordinates.
Since this file is written in html with script tags to import the script, it interprets those scripts as javascript, and because of that, it stops working.
Firebug shows me that the file is found and shows me the content of it, but in the .js script file i have to do stuff like this to use node.js modules, that are necessary for my functions to work.
var fs = require('node-fs');
or like this to use functions in the file from other modules.
exports.someFunction = someFunction;
So, when the browser loads the map and tries to find the coordinates that i'm passing from the script that reads the .txt file where they're saved, it finds any of those two things and can't process them, because they're node.js, not plain javascript.
So my question is, how do i get javascript to recognize require() and exports when they're node.js commands?

If I understand you correctly and you're trying to use Node.js module system in browsers, that won't work. Browsers doesn't implement such features natively. Script files can only be included to the page with script tags or some kind of ajax system that fetches the files from the server. There are few such a libraries that provide such a functionalities.
RequireJS might be what you're looking for the client side though.
RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. Using a modular script loader like RequireJS will improve the speed and quality of your code.
Also note that some of the Node.js features are not possible in the browsers. For example the direct file system access is not possible in the browsers, it would be a huge security risk.
Most javascript modules that provide both server and client side functionalities explicitly mentions it and provide separate script files for both sides.
Also if you only want to send coordinate data from the server to the client, then it might be good to use Ajax or WebSocket to send the data in a JSON format.

Related

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.

TypeScript + NW.js: need namespaces for browser, need commonjs for nodejs - How do I get both?

I'm writing an application using NW.js and TypeScript. I would like to use one class per file.
To access node.js modules, I need to use require(). However, the document context within a require()d module is no longer the browser's document context.
It seems to me that I need to stick to namespaces to be able to access the browser DOM from my application.
How can I combine those two module styles in one TypeScript application? When trying to use namespace together with require, my types suddenly lose visibility across files (because the compiler goes into external module mode.)
It seems to me that I need to stick to namespaces to be able to access the browser DOM from my application
No. You can use a module loader like webpack to bundle the portions that the browser needs into a single bundle.js while still keeping all your code base in commonjs mode.
More
A quickstart is available : https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html
More More
I am actually creating an OSS editor that demonstrates this : http://alm.tools/. The whole project (backend + server) has a single tsconfig.json file.

What is the difference between Node.js require() and RequireJS require()?

I setup a website with regular client side RequireJS today. Than I did some research on node, got that installed and setup my first module in node. When I setup the first require, I loaded the Require.JS file and I get all that. The thing that is confusing me is, I created a file called test.js and within that I am including:
var require = require("requirejs");
which is actually including the node require, not the original require library I was using right?
So like are they completely different? Can they used together?
Doesn't Node already have a module loader?
Yes Node does.
That loader uses the CommonJS module format. The
CommonJS module format is non-optimal for the browser, and I do not
agree with some of the trade-offs made in the CommonJS module format.
By using RequireJS on the server, you can use one format for all your
modules, whether they are running server side or in the browser. That
way you can preserve the speed benefits and easy debugging you get
with RequireJS in the browser, and not have to worry about extra
translation costs for moving between two formats. If you want to use
define() for your modules but still run them in Node without needing
to run RequireJS on the server, see the section below about using
amdefine.
Source: http://requirejs.org/docs/node.html#1

Get a JSON response and save to file using require.js

Im trying to make a plugin for require.js that allows me to call an external api, convert the json response and save it to a file.
Problems:
Im not sure if I am writing the plugin correctly
I cant seem to use node filesystem - though i am using r.js
I am hoping to do this on build, so that the file is ready before concat method happens (putting all files into one)
Is this even possible? Should I use a grunt task instead?
Any pointers or examples or tutorials or anything would be really useful.
In the end I used, https://npmjs.org/package/grunt-curl.
Was alot easier, and just modified the file a bit to wrap the response in define();
It allows the files to be downloaded on build and required later in the app

Combining NodeJS files

Standard NodeJS app utilising module.exports to make the code modular. We'd like to combine all of those files together into a single file a la RequireJS on the client side (which is what we use there). Does anyone know an easy way to do this server-side without wrapping everything in more module wrappers (e.g. RequireJS) e.g. command-line tool ?
Ta
N
If you want to use node.js modules on the client side without additional wrappers (like RequireJS), you'll want to look into using browserify:
https://github.com/substack/node-browserify
It's a very useful build tool that allows you to use node-style require() statements and native node.js modules on the client side. Or, to quote from the github page itself:
Make node-style require() work in the browser with a server-side build step, as if by magic!

Resources