0

I have started a POST server using node Js. I want to access the POST service through html file.

<html>
   <body>

<script type="text/javascript">  
function PostMethod(){  
var http = require('http');

//The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
  host: 'localhost',
  port:8081,
  path: '/',
 method: 'POST'
};

callback = function(response) {
  var str = '';

  //another chunk of data has been recieved, so append it to `str`
  response.on('data', function (chunk) {
    str += chunk;
  });

  //the whole response has been recieved, so we just print it out here
  response.on('end', function () {
    alert(str);
  });
}

http.request(options, callback).end();
}  
</script>  
<form>  
Enter No:<input type="text" id="number" name="number"/><br/>  
<input type="button" value="click" onclick="PostMethod()"/>  
</form>

   </body>
</html>

This is not working. But iF I seperatly run the script in node js, the post reply I am getting

1
  • node code isn't meant to run at the client side. its server side (backend) Commented Nov 27, 2016 at 14:49

1 Answer 1

0

You can't use node modules directly in browser. You can try this one. https://github.com/substack/http-browserify

Have a look at this. http://browserify.org/

Sign up to request clarification or add additional context in comments.

1 Comment

Other common alternatives would be a simple plain old HTML form (no JavaScript needed) or jQuery.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.