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.
Related
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.
for example:
If i am using an npm package, i can only use it on server side js. Does that mean one will always have to send data through ajax post/get request for example to server side do the calculation then send data back? Is there a better way to accomplish this?
I am aware of how react works etc and i am aware of workarounds to get require to work browser side, but for sake of understanding workflow and how it should all be set up I ask this question.
I am currently using express with nodejs and using ajax calls to talk to server side js and send info back. So want to know if there is a better way of doing this?
No, there's no "better" way to send data from the client browser to the server than an ajax call. Alternativelly you can use a normal form, or url parameters to pass the server some variable with the page load request.
Depending on the npm package you are using to do your calculations, you can just serve that .js library up along with your .html page by moving that script into a static assets folder and referencing it with a <script> tag. Then, you can do the calculation entirely in the browser (client-side) assuming you don't need any other I/O (db, etc.).
I've been through a number of Node.js, Express, and other tutorials/posts, and I'm struggling with how to think about connecting to a database on various pages throughout a webapp.
I would like to run a Node.js app (with a server.js file that connects to a database) and then query that database as needed on every page throughout the app.
So if I have an inventory.html page I should be able to have javascript that queries the inventory table and displays various inventory items throughout that html page.
Problem #1. I can't find a way to use mysql on any client-side pages, since javascript can't use node's require() function client-side. As detailed in this StackOverflow post ("require is not defined").
Problem #2. I can't figure out an elegant way to pass a database connection to other pages in my app. A page can send a POST request back to the server.js file, but this really isn't as flexible as I want.
I'm really looking for the modern, preferred way to do a bunch of PHP scripting in my Node app. Can anyone guide me to the right way to do this? Thank you!
You just can't directly call mysql from the client. Even if it worked imagine that anybody could modify the SQL queries and access all your data.
The only way how to do it is this:
js client app ------> js server app -------> mysql
You just must have 2 apps: one running in the user's browser sending requests to the server and the other running on the server answering the requests.
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.
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