0

I have been looking for a solution for this days ago... so basically I have 2 files: index.html and hellonode.js. Index.html has a div with text and a button that is supposed to make a request to hellonode.js when clicked. hellonode.js is supposed to receive requests and send a response to them.

hellonode.js:

var http= require('http');


function onRequest(request, response){
    console.log("request has been received");

response.writeHead(200, {"Context-Type": "text/plain"});
    response.write("<h1>response here!</h1>");
    response.end();


}


http.createServer(onRequest).listen(8888);

console.log("server is running");

here is my index.html:

<!DOCTYPE html>
<html>
<head>
<script>
    function loadXMLDoc() {
        var xmlhttp;

            xmlhttp = new XMLHttpRequest();


        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById('myDiv').innerHTML=xmlhttp.responseText;
            }
        }

        xmlhttp.open("GET", "hellonode.js", true);
        xmlhttp.send();

    }

</script>
</head>
<body>
    <div id="myDiv">
        <h2>content</h2>
    </div>
    <button type="button" onclick="loadXMLDoc()">Your response content should appear here</button>
</body>
</html> 

Im using node js and a apache server (the binary version on Apachelounge.com), and put inside the htdocs folder those 2 files and then start my server, so when I access 192.168.0.102 from my other computers, I get the index.html screen. Everything is fine until here. When I click the button and send the request, the response that I receive is the entire javascript code of hellonode.js. Why is that happening? I have a feeling that my hellonode.js isnt even receiving my request, and that the index.html is just reading the hellonode's content and showing it istead of actually sending a request to it. Im new to this server-side stuff. Thanks.

user3658801
  • 127
  • 1
  • 4
  • Apache is server and with nodejs you can create server. Now you are requesting the file `hellonode.js` when you should request some url. Read this https://expressjs.com/ and stop the apache.. :) – Hardy Aug 06 '16 at 22:22
  • `Context-Type` — You misspelt *Content* – Quentin Aug 06 '16 at 22:28
  • you are expecting your hellonode.js code to work like a .php file ... but this is not the case. If you have nodejs installed on your computer you should be able to start your code with `node hellonode.js` and then point your browser to `127.0.0.1:8080` ... this will give you a page w/ `

    response here!

    `
    – CaffeineAddiction Aug 07 '16 at 00:48

6 Answers6

1

Apache HTTPD is an HTTP server. The JavaScript you have written is also an HTTP server.

You are making the request to Apache HTTP for the file containing JavaScript program.

You should be running the JavaScript program via Node.js (e.g. from the command line of the server) and then using XMLHttpRequest to make the request to http://example.com:8888/

Note, you will then run into this error, so you will need to modify the JavaScript program to include CORS headers in the response. (Or you could use Apache's mod_proxy to let you make the request to Apache and then forward it to the JavaScript server).

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Or serve your html from your nodejs server – user3791775 Aug 06 '16 at 22:35
  • Thank you, now im using just node js as a server and I wrote my ip with the port instead of hellonode.js on the following line: `xmlhttp.open("GET", "hellonode.js", true);`, but now Im having trouble with the CORS stuff. Where should i put the CORS headers in my code, and how they are exactly? – user3658801 Aug 07 '16 at 01:26
0

This is happening because you are requesting the file hellonode.js using a GET request.

You need to set up and end point which you can send the request to. For example.

You need to setup a node server not an apache one.

Duncan
  • 226
  • 1
  • 11
0

You're correct. By sending an XHR request to hellonode.js, it's just sending a request to retrieve the file, and then returning all of the contents of said file.

It looks like what you're trying to do is run a Node server. You're going to need to configure your server to be able to serve the Node app, instead of running it through Apache (unless you want to serve a Node app with Apache).

To do it locally, you should run node hellonode.js, and it will spin up a server using Node, and be able to serve resources like you're expecting.

(Still, in that case, though, sending an XHR request to a file is just going to return the whole file without running it)

Community
  • 1
  • 1
0

Nodejs is not intended to be run with apache. Apache shows your html page properly because it is meant to serve html pages -apart from PHP scripts-, however, is not intended to serve node scripts. So the request is returning your node scripts just as a plain text, like when you request a .css or .js file asset from a web page in the browser.

You must set up a node script explicitly invoking it with the node command: node hellonode.js

Or in linux: nodejs hellonode.js

Then, if your request and your server side script are properly coded, nodejs will actually listen to the request and send the proper response.

However, keep in mind that serving pages with node is not as straightforward than with apache, and educate yourself in services like nodemon or pm2 will sure pay you off.

Sergeon
  • 6,638
  • 2
  • 23
  • 43
  • "Nodejs is not intended to be run with apache". This is not completely true. You can use apache for serving static files and as a proxy for node.js. – Ram Aug 06 '16 at 22:49
0

As an example xmlhttp.open("GET", "hellonode.js", true); can be xmlhttp.open("GET", "http://www.thomas-bayer.com/sqlrest/", true);

naga
  • 29
  • 5
0

Once you type in your terminal:

node hellonode.js 

the Nodejs server is running on your current directory and hellonode.js file is used to display the result when you browser to : localhost:8888 in other words your hellonode.js file is playing the role of index.php in the classical php application.

To achieve what you want you can use the built-in http module, or use a framework like Expressjs or others.

ismnoiet
  • 4,129
  • 24
  • 30