I just started learning AngularJS for my website development, and its perfect for what I need. However, I'm very new to web programming and I've been looking for the answer to my question for a while now, and I couldn't get a clear response. I'm wondering how I can handle incoming HTTP Requests in my AngularJS application.
For example, I'm going to have a computer running a program that is going to be constantly sending HTTP Post requests with data to the web server running my Angular app. I want to be able to parse the information in the app, and display the data using Angular's data binding. But, I can't figure out how in Angular you can listen for incoming HTTP requests, parse the data, and send back a response.
AngularJS is designed for front-end web apps. You use to make an HTTP request from the users web browser to a webserver listening for such calls. But angular itself can't listen for them.
If you want to be able to listen for a HTTP request you will want to try either apache/php or nodejs/express. nodejs will probably work best for you because you can program in javascript.
Related
I am working on a web proxy right now and it works, but I need some Javascript that could see all incoming XML, Fetch, and websocket request and rewrite them. Only problem is that I don't know how to. I really need this in pure javascript. I apologize if I can't show any code here.
if you want to read the content of the requests, you need to set up a web server.
you can use http and websocket module to set up a simple server,
but I recommend using a backend framework, such as express.
My question is about:
I am currently trying to connect my electron application to my node server to get data from node.js server and print that data to my electron application. However I don't know how to do it. So could anyone help me:
To get data from node.js server into electron app?
The way you get data from any node.js server is you make a request to that server specifying in the request what you're asking for and the server responds with the appropriate data.
There are literally thousands of ways to physically make the request. The classic way in these days of web technologies is to make an http request from your electron app to an http server in your node.js server. You can make such a request from electron using the request() library.
You would then have an http server as part of your node.js server and you'd specify routes in that http server that handle the requests your electron client is making, fetch the desired data and send the data back as the response. In the node.js world, you can create a simple http server and a few request handlers in a dozen lines of code using the Express library.
This is the general approach. Further details on the exact request to make and URLs to use are dependent upon the details of what you're trying to do and the design you choose, none of which you've shared with us.
I am building a web application in which i have to make a lot of $http requests to server. i am using AngularJS in front-end and ExpressJS in back-end. i am making $http requests something like this :
but the problem is , my app is very slow. There is a glitch in each section between opening the page and retrieving the data.
Is the way i am making $http requests is architecturally correct or i am doing something wrong.
How to decrease Time To First Byte (TTFB)?
You should find out if the server is slow while handling the request by printing some log on server side.
I guess the problem is not on AngularJS, but the server side.
Is it possible to have a web service notify web clients when something occurs via HTTP request/response? Right now my client has to poll the server using HTTP Requests at an interval to check for updates, but it would be far more convenient if I could register a javascript function to the server from my client and have it be simply called when server state changes.
Note that the web service is written in Python and utilizes HTTP APIs (I think it uses cherrypy, if that's relevant).
If this is possible could someone point me to some tutorial that explains how to do this or give me a basic understanding of how this can be accomplished?
Look into Comet
You can't start a request from the server to the client. You can only poll the server (which you did), use a hidden iframe, use a plugin, or use the new HTML5 WebSockets which allows the server to send a message to the client.
If you are into Node.JS, look into socket.io and hook.io
https://github.com/hookio
https://socket.io
I've just started learning Node.js and was very interested in its real-time capabilities, especially with Socket.io. Since then, I've written a very basic script to connect to Twitter's streaming server and broadcast tweets to all connected clients.
To build that, I used http.createClient to connect to stream.twitter.com and added in the relevant response and data event handlers. Everything works quite well.
Obviously, Twitter's Streaming API pretty much outputs an infinitely loading webpage and what why using a data event handler works fairly well with it. However, is it possible to make other types of websites 'streamable'?
For example, if a client (browser) updates a website periodically using an XMLHTTPRequest, would it be possible to track the output of those requests using the HTTP API of Node.js? Or similar Node.js extension?
Thanks.
websites do not periodically use XMLHTTPRequest. Clients periodically send XMLHTTPRequests to an URL.
A simple call to http.request(options, callback) with the correct headers should emulate XHR's. Most of these servers will also accept normal POST or GET requests.
If you want node.js to connect to a server and simulate a browser then something like zombie would serve you well. It claims to support XMLHTTPRequest.
The best case for you would be to use web-sockets between your dashboard and node server. This way node will be notified immediately that something has updated at your dashboard ( I am assuming that you can modify your dashboard a bit to accept such connections, won't be difficult as long as you have access).
Then you can use long polling at client-end i.e. send a request to the node server and wait. Node will receive the request and then register an event to it. The moment it receives the updates from dashboard, it'll fire the event which will send the response to all the clients one by one waiting.
I would recommend take a look at http://github.com/andrewdavey/vogue . It does something similar but the functionality is ofcourse different.