4

I copy/pasted a node js hello world example into an html file

<html>
<head>
<script>
var http = require("http");

http.createServer(function (request, response) {

   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});

   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

</script>

</head>
</html>

I'm using a Chrome app called OK 200! Server to make this page available at http://127.0.0.1:8887/ The ideal solution would work on a Google Chromebook.

When I run this it gives error:

Uncaught ReferenceError: require is not defined(…)

So that implies several features unique to node won't be embedded in Chrome. Is there an extension, chrome app, or native client app that would make those features available and still leverage the v8 engine already embedded in Chrome?

Darian311
  • 764
  • 7
  • 20
  • 1
    Not in a plain Chrome browser. A standard browser just doesn't have the capabilities to run a server of any kind, much less access the rest of node.js functionality. You would need naive code (separate app or plugin) that could add required features to make creating a server possible. – jfriend00 Nov 12 '16 at 21:04
  • https://github.com/kzahel/web-server-chrome – guest271314 Nov 12 '16 at 21:30
  • The process should be possible https://www.chromium.org/developers/design-documents/sync, http://stackoverflow.com/questions/2162173/is-google-chrome-embeddable, http://stackoverflow.com/questions/35899536/method-for-streaming-data-from-browser-to-server-via-http. You can also build your own version of chromium https://www.chromium.org/developers/testing/chromium-build-infrastructure. – guest271314 Nov 12 '16 at 21:48

2 Answers2

2

I'm not really sure about your goal ? Are you trying to run a server from a client web browser ? I'm not sure this is possible for many reason (as security for example)... I think you need to read this !

If you're just trying to run a node server on your localhost (127.0.0.1), you need to install nodejs on your computer Node.js, then you need to save your nodejs example in javascript file (named "myServer.js" for example) and run node in CLI using:

node path/to/myServer.js

And about:

Uncaught ReferenceError: require is not defined

you should see #19059580

Best regards, Tristan

Community
  • 1
  • 1
Tristan
  • 170
  • 1
  • 10
  • My goal is to see if there's a way to have a dynamic web server run on a Google Chromebook? – Darian311 Nov 13 '16 at 16:50
  • So take a look [here](http://jeremyckahn.github.io/blog/2013/02/09/setting-up-a-local-development-environment-in-chrome-os/) – Tristan Nov 13 '16 at 23:21
1

You could use something like electron.

It basically gives you node.js and chromium and enables you to build cross-platform desktop apps.

mfreitas
  • 2,303
  • 3
  • 28
  • 42