Search code examples
javascriptnode.jsbrowserify

making http server with node and browserify


im making a mobile app that require a http server so im trying to make it with browserify so this is what im doing, i got this server.js

    var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

then i do this

$ browserify server.js > browserify.js

then i enmbeded to my html like this

<html>
  <body>
    <script src="https.js"></script>
  </body>
</html>

then when i run the html i get this error

Uncaught TypeError: http.createServer is not a function

how can i get a http server working on the browser ?

by the way i got http-browserify installed


Solution

  • Apologies if I'm reading this question wrong, but you can't create an http server from within a browser. That http.createServer call is only meant to be called (and is only defined) in node.js.

    So you would need to run your script from node from the command line, like this:

    node https.js