1

I know how to send an http request to a server using angular js. With the promise returned, I know how to listen for a response and manipulate the ui thereafter. But this approach cannot be used for what I have in mind.

However, what I cannot figure out, is how to send a request to a website.

I have a server localhost:800/receiveData which receives a POST request and then manipulate the UI and DoM on the angularjs site

app.get('/', function(req,res){
    res.sendFile(__dirname+'/index.html')
})

app.post('/receiveData', function(req,res){
    var data = req.body.data
    // assume data is a boolean

    if(data){
        //show a view in index.html using angular js or anything else
    }else {
        //show a different view in index.html
    }
});

Any help will be greatly appreciated. I have a need for angular js. Having a SPA is imperative. I am completely open to adding additional stacks if neccessary.

EDIT: As pointed out by MarcoS, manipulation of dom should ideally not happen from the server side. I am combining IPFS with node js and angular js to develop a single page application. The swarm of nodes set up using IPFS has an open line of communication with my server (by design). Based on packets of data sent via the comm line to my server, I need to convey messages to the user via the index.html.

3
  • you would really have to decide who does the job of view templating - serverside or front end. Commented Jul 13, 2016 at 10:05
  • Are you asking how to update the view on the client in response to a different client making an HTTP request to the server? As, for example, you might want for a chat system (so Bob sends a chat message to the server and then the server tells Alice's browser there is a new message and it should update the display)? Commented Jul 13, 2016 at 10:08
  • something like that could work. I have experimented a bit with socket.io . What do you have in mind? Commented Jul 13, 2016 at 10:14

1 Answer 1

2

I think your approach is wrong: on server-side, you should NOT manipulate the UI and DOM...
You should just do server activity (update a database, send an email, ..., return a static page).
Then you can output a result (JSON/XML/... format) for your client-side calling script to read.

Following OP edit, what I do understand is he wants server push to the client.
To get serve side pushes, you should poll on the client.
In a controller:

function getServerState(changeState) {
  return $http.get("/receiveData").then(function(res) {
    changeState(res.data); // notify the watcher
  }).catch(function(e) {
    /* handle errors here */
  }).then(function() {
    return getServerState(changeState); // poll again when done call
  });
}

Consuming it this way:

getServerState(function(status) {
  $scope.foo = status; // changes to `foo` $scope variable will reflect instantly on the client
});

And, server side:

app.post('/receiveData', function(req, res) {
  var data = req.body.data; // assume data is a boolean
  res.end(JSON.stringify(data);
});
Sign up to request clarification or add additional context in comments.

2 Comments

I am trying to combine IPFS nodes with nodejs and angularjs. The IPFS node can only communicate via an open line of communication with the server. I have added an Edit with a better explanation. Would really like if you could see it. Thanks
I see. Updating my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.