How to make a UI for Node scripts - javascript

I have this node script where it does an API call and receive the response. So what I want to do is to make a UI that controls when the script works. For example when I click a button (HTML) the script starts and then prints the response into a textbox. Is there any way to do this?

You need something to render the HTML and send a message to your Node.js program and your Node.js program needs some way to understand that message.
The most common approach would be to write a web service (usually using Express.js unless you are using React/Vue in which case Next.js/Nuxt become more interesting) and then communicate with it using Ajax (typically the fetch API). Other options would be form submissions or web sockets.
Less common would be to use a framework such as Electron.js or OpenFin to run a desktop application with an embedded HTML renderer. They then have their own APIs to communicate with the Node.js portion of the application.

Related

Jmeter Script Replay Issue - Please enable JavaScript to view Page Content

I am facing an issue in simulating below scenario in JMeter script. Appreciate if anyone of you can help with a solution.
I am trying to create JMeter script for a form submission flow which is a .NET application. One of the HTTP Request Samplers is getting redirected to a different HTTP request. JMeter script replay is able to redirect to correct HTTP request; however, it doesn’t provide required HTTP response.
It fails with the message – “Please enable JavaScript to view the page content. Your support ID is: 7865380748200702010”
While recording the script, it gives proper response with .net variables such as View State, View State Generator, Event Validation etc.
Please help me if you have got this earlier.
Most probably you're not sending the right requests because your script is missing or doesn't have properly implemented correlation of the dynamic parameters
In the vast majority of cases you won't be able to replay the recorded test scenario, in your case due to incorrect hard-coded recorded values of these View State, View State Generator, Event Validation, etc.
While browser is sending these variables automatically for JMeter you need to extract them from the previous response using a suitable PostProcessor (I would recommend CSS Selector Extractor), convert them into JMeter Variables and replace hard-coded values with the variables. You can see ASP.NET Login Testing with JMeter article for example correlation of these .NET web applications dynamic parameters.
With regards to JavaScript in general, as per Apache JMeter project main page
JMeter is not a browser, it works at protocol level. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages. Nor does it render the HTML pages as a browser does (it's possible to view the response as HTML etc., but the timings are not included in any samples, and only one sample in one thread is ever displayed at a time).
so if a part of your page is being loaded by JavaScript (i.e. using AJAX technology) JMeter again won't execute this request automatically, you will need to properly simulate it

Passing NodeJS data to Javascript

I have a web application with a client that receives data from a server. I have the data in NodeJS, but I want to pass the data to a Javascript file. The Javascript file is included in a HTML file, so I can't make the files communicate with eachother.
I am new to NodeJS, so it can be a stupid question, but anyones help is appreciated
This is for a project where I need have a data stream, and I need to pass it into a web application. I tried to pass the data to different page inside my application and then I tried to get that data on that page inside my web application via Javascript, but I couldn't make that work. I'm not even sure if its possible at this point.
Your node server can't communicate with your front-end without a specific way of communication like websocket, you have many other way to communicate with your front-end as node-server, take a look at server send event for example.
By the way your front-end can call your node server more easely with a get request as said #tomerpacific in comment.
For that you have to open a route with your express app. Routing with express
And for call it on a GET request, for that you can use the XMLHttpRequest, and if you have implemented jQuery on your front, you can use Ajax jQuery.

Suggestion: Single Page application architecture issue

I have written a web app (Single Page application) which has only frontend technologies involved (Vuejs) and when I compile it, it will ultimately generate web pages (only HTML and JS). I can run this app anywhere by opening the index page.I am consuming REST API powered by oAuth on this SPA (making direct Ajax call to REST API endpoints).
But the problem is, My lead developer is saying the SPA must be powered by back-end service (Server) for example nodejs, apache. And the backend should make call to the REST APIs not directly Ajax calls from the browser (Frontend JS ajax). My SPA app runs anywhere and works perfectly on browsers even without any server.
My question is, do I really need to render and run my SPA using webserver, whats the reasons behind making my SPA (Plain html, js) app server powered??
Also please suggest me, if people simply write app using JS and HTML (pure front end) and upload on the server and point a domain name to that html-js web app which will be consuming remote REST APIs.
Thank you for making my doubts clear in advance.
I have remote REST API provider, suggest me best way to write an SPA to consume that remote APIs.
There may be some reasons to setup a back-end service, for example:
Hide REST API endpoints
Setup your own caching / throttling / failovers etc. to REST API endpoints
Override / control REST API responses / requests
Still, you can use only pure html+js SPA, but adding back-end service gives you additional options, not possible to achieve on front-end.

Web scraping in AngularJS application

I have an AngularJS application which currently uses http.get to get JSON data from an API. This works great.
I'm wondering if I can pass a static webpage URL, and scrape the results using the response from http.get?
I've seen tutorials on web scraping with Node and JavaScript libraries like ScraperJS, but I haven't been able to successfully use these in an Angular (client-side) application. Is there anyway to use a JavaScript web scraping library in Angular?
There is no direct way to do so because this has nothing to do with Angular at all. Client-side JavaScript is just that. It runs on the client. What you need to do involves making an HTTP call for request URL and retrieving the HTML from a site, then parsing that HTML for various meta-data. That needs to be done via a server-side call to the site to load the data from the remote site.

NodeJs back end code structure

I'm quite new to web applications and have decided to create a single page web app hosted on Heroku.
My understanding of this web app is as follows:
Client side (AngularJs) has input text box, once button press it requests server side endpoint
Server (NodeJs) uses data from client to call external API (e.g imgur API) and returns json
Server processes json and responds to client with information
Client uses server response to render user interface
Main Concerns
Best practices for external API calling: Should I have an API wrapper class that allows me to call custom methods that return specific external api calls?
How should I handle http error responses?: I understand that NodeJs is async by nature and all http calls are done async as well. If there are multiple responses, error or success, how do I go about handling them all without doing a custom set of ".error()" and ."success()" methods for each call?
Furthermore
I cannot seem to find a good reference material for a simple NodeJs back end like the one I described. Please direct me if there are any.
I recommend looking at this Scotch.io article for creating a Single Page MEAN Application:
Setting Up a Single Page MEAN Application Starter Kit

Categories