Example WriteBack with nodejs - javascript

I'm just starting to use nodejs. I've done some basic learning course but I miss any basic example to start becaue I'm having problems to deploy an scenario for the first time.
The scenario I need to cover is:
I just need to write and read data from an Access.MDB file.
I have a basic example that works if I execute the js with the sentence \node appnodemdb.js from the command line.
But I need to execute it from another js in a web.
So I'd appreciate a lot if anyone could send me an example about execute it from an other js without the command line.
Thanks a lot for your help!

You can't call directly some NodeJS code with the browser. To achieve what you want, the simplest way to do is:
Create an HTTP server with a URL to will launch your write/write action and return some value (with Express for example).
This URL will be called by your JS in the browser (using XHR/Ajax) and process the data returned.
They are tons of alternative to do this, but since you tell us that you are beginner, I suggest this approach.

Related

How do I add this JavaScript code to Zapier, to issue a 3rd party gift?

My eCommerce website needs to run JavaScript code so a 3rd party can send the customer a gift. The JavaScript code worked on the Thank You page, but I need to have a few-day delay before the script runs, so I'm trying to do this with Zapier instead. But Zapier is giving the following error when the same code is used there:
The run javascript could not be sent to Code by Zapier. SyntaxError: Unexpected token <
Zapier Support could not help, and I couldn't find the solution in Zapier's documentation.
Can someone please help modify the short code below, so that it can be run by Zapier?
<script src="https://members.thirdparty.com/jsapi/fbDE_Voucher.min.js"></script>
<script>
window.onload = function(){
var fdDE_detail = {
"fbDE_sender":'12345-67890',
"fbDE_fullname":"'"+inputData.CustName+"'",
"fbDE_email":"'"+inputData.CustEmail+"'",
"fbDE_amount":inputData.VoucherValue,
"fbDE_business":54321,
"fbDE_message":'Thank you for your purchase!'
}
fbDEVoucher(fdDE_detail);
}
</script>
The variables CustName, CustEmail, and VoucherValue are from prior Zapier steps. The first 2 need to be within single quotes, and the third is an integer (not requiring quotes). Please see image below:
JavaScript code for Zapier
Does anyone know how to fix the above error (in bold) so this code runs?
Appreciate your help! Thanks.
Zapier's code runs in Node.js, which is a different environment than the browser-based JS your code was running in before.
First off, <script> tags aren't available, since that's HTML. Whatever code comes in through fbDE_Voucher.min.js will need to be loaded separately. If it's not too long, you can paste it into the code window (but there's a better solution, see below).
There's also no window global, but you don't need it anyway. So to get this close to working, it would be something like:
// doesn't work, fbDEVoucher is undefined
fbDEVoucher({
fbDE_sender: "12345-67890",
fbDE_fullname: inputData.CustName, // doesn't need to be re-wrapped in a string
fbDE_email: inputData.CustEmail,
fbDE_amount: inputData.VoucherValue,
fbDE_business: 54321,
fbDE_message: "Thank you for your purchase!",
});
That's all you would need, assuming your function is already defined.
The best option for you here is to create a custom Zapier integration. This gives you a more full-featured JS environment than a Code by Zapier step, plus dramatically more control. You can include the aforementioned file and call the function as needed. There's docs about that here: https://platform.zapier.com/

parsing different javascript from same source

i know these things can be done easily through php but in shopify store i saw javascript attached like this async.
https:\/\/sometime.com\/shopify\/script.js?shop=storename1.myshopify.com
https:\/\/sometime.com\/shopify\/script.js?shop=storename2.myshopify.com
https:\/\/sometime.com\/shopify\/script.js?shop=storename3.myshopify.com
and to my surprize all three code was different.
anyone can tell what is happening behind the scene. how it is possible that different javascript is passes from same script.js ??
any help will be great learning
Here
https:\/\/sometime.com\/shopify\/script.js
is essentially not like there is a file script.js inside directory /shopify, rather it is a route. And then the shop acts here as a query parameter for GET request, the server reads it and returns the response accordingly.

How to develop a javascript library from an already existing npm module (codius)

never done this before.
I'm using https://github.com/codius/codius-host. CodiuĀ§ development has been abandoned, but I want to salvage part of it to use for my own project. I really need to be able to run codius commands from browser, so I need to develop a library or what you call it.
var codius = require('codius')
codius.upload({host: http://contract.host}
codius-host comes packed with command-line integration,
$ CODIUS_HOST=https://codius.host codius upload
How do I make a .js script do what the command-line command does ?
also posted on https://stackoverflow.com/questions/31126511/if-i-have-a-npm-tool-that-uses-comman-line-commands-how-can-i-create-a-javascri
hard time asking this questions since don't know where to start. help.
Assuming that you have access to the codius-host source code you should find the piece of code which manages the command line arguments. I am sure that they do handle the command and the command line arguments from an entry module/function and than later delegate the real job to a different module/function. What you need to do is to provide correct parameters to the functions which the function/module that handles command line argument calls with command line parameters.
In addition to that there are some nodejs libraries that might imitate a command line call from the program itself. One of those I know is shelljs:
https://www.npmjs.com/package/shelljs
You might want to check this out as well. With this one without bothering with the source code you might be able to imitate command line behaviour.

How to run js code pulled from server side in angularjs?

I am using Laravel as my back-end and Angularjs as my front-end for my website. I need to pull some js code (advertisement purposes, like Adsense) from server side and run it in Angularjs.
I got some ideas from https://www.ng-book.com/p/Security/ . First I use $sce to make the js code trusted so that angularjs can run it,
$scope.safeJsCode = $sce.trustAsJs($scope.unsafeJsCode);
then I use ng-load to eval() the safeJsCode
<script ng-load="run()"> </script>
$scode.run() = function()
{
eval($scope.safeJsCode.toString());
};
However, I was not able to run it. Can someone give me some ideas on how to solve this predicament? Thanks!
So if I understand correctly, you think that ng-load can call your 'run' function.
Empty script tag won't fire a 'load' event, so no wonder that it is not being executed.
If you really need to dynamically load some script and execute it you don't need no script tags-just as soon as you fetch script from the server and get safeJsCode, eval it.
Now I don't expect you to heed my advice, but instead of doing eval(), you should really think about a way how you can achieve with just data. In most cases this can be solved by just having your own smart code.
There are case which comes to mind- like a dashboard with customizable widgets, where people can write 3rd party widgets. Problem is, these never catch on and they are very prone to bugs and usually are sluggish.
If you trully need dynamicity though, you could try loading those script with https://github.com/systemjs/systemjs

How do you get a return value in javascript from a Stripes ActionBean?

I am attempting to use Google's Channel API to construct a 2-player game session. I am using a JavaScript function to start opening the channel, and I have a Stripes ActionBean that opens the channel and obtains a new Token for the specific user.
How do I call the ActionBean from JavaScript and get the "String token" from the ActionBean? ${actionBean.token} did not work in the .js file.
PS: I am new to these languages and would appreciate the time and effort invested in answering my question. Thanks !
Edit: Thank you ! I have read the proposed links and I am excited about the JavaScriptResolution. How do I receive the JavaScriptResolution in javascript? Can anyone kindly provide a code snippet perhaps?
Unless you are generating the Javascript in the actionBean that generates the page with the Javascript, the ${actionBean.token} will not work. In that case you need Ajax, you need to make the http request in Javascript. You might want to take a look at this Stripes Ajax example:
Stripes Ajax Example
You might also want to use a JavaScriptResolution to return objects from an actionBean to Javascript, see example code:
Amis JavaScriptResolution Example

Categories

Resources