27243

node.js and HTML page? How to combine?

Question:

I would like to establish a communication between a HTML-page with some JavaScript code and a simple webserver based on node.js. My first node program (demo) looks like this and i see it working by calling http:…name=someName from the web browser! Fantastic!

http = require('http'); var url = require('url'); var htmlencode = require('htmlencode'); var port = process.env.PORT || 3000; var request = function(req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); var parsed_url = url.parse(req.url, true) res.write(""); res.write("

Hello World

"); res.write("<h2>Hello " + htmlencode.htmlEncode(parsed_url.query.name)+"</h2>"); res.write(""); res.end(); }; var server = http.createServer(request); server.listen(port, function() { console.log("Call server with http://localhost:" + port +"?name="); });

Now i have my HTTP-page and i would like to start with that and then let node handle the requests. I assume, i have to load it from node, but how? Or do i simply load it by calling the page and then switch to node, but how again? I guess communication from my page is done by AJAX?

Thanks for any help and i would appreciate some sample code! Peter

Answer1:

<blockquote>

Now i have my HTTP-page and i would like to start with that and then let node handle the requests. I assume, i have to load it from node, but how?

</blockquote>

You've written an anonymous function and assigned it to a variable called request. You've set up your program so every time your server gets a request, that function creates a response to it.

If you want to respond to requests for your HTML page with your HTML page, then you need to change that function so it responds with the HTML page you want.

Presumably, that will be writing a condition to check what URL was requested (the details are in the object assigned to req) and then either do what you are doing now, or read the HTML file and send its contents.

<blockquote>

Or do i simply load it by calling the page and then switch to node, but how again?

</blockquote>

Then you run two HTTP servers and will have to use absolute URLs to reference one from the other.

<blockquote>

I guess communication from my page is done by AJAX?

</blockquote>

There's no need to involve client-side JavaScript at all. A regular form submission should do fine.

Recommend

  • Connect to node js server running on vagrant machine
  • Formidable Upload Not Working: 'files\" undefined, no error
  • nodejs error: module.js:340 throw err;
  • Docker not expose ports for node and webpack dev-server
  • Fancy dynamic list in Android: TableLayout vs ListView
  • Global session variable in express.js route?
  • Change Divider Color Android DatePicker Dialog
  • angular2 http.post() to local json file
  • Translate animation to correct position in Android
  • Loop animation drawable
  • Rails 4.1 environment variables not reloading
  • Netlink sockets and libnl - nl_recvmsgs_default returning -16 (EBUSY)
  • Gray line inside cardview :)?
  • Express JS Display Data By ID
  • How to create a custom Powershell operator?
  • How to resolve permission denied maybe missing internet permission?
  • Prevent Tomcat from caching request during starup
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • Access Android Market through SSH tunnel
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • Code in Job's Script Block after Start-Process Does not Execute
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to get a value (ex: baseURL) in every Karate feature?
  • Symfony2: How to get request parameter
  • Web-crawler for facebook in python
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • How to get icons for entities from eclipse?
  • log4net write single file for each call to log.info
  • trying to dynamically update Highchart column chart but series undefined
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • Getting error when using KSoap library to consume .NET web services
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • JaxB to read class hierarchy
  • Binding checkboxes to object values in AngularJs
  • java string with new operator and a literal