I have defined the following express server, which is hosted on localhost:
const express = require('express');
const http = require('http');
var app = express();
app.get('/', function(req, res){
res.status(200);
res.send("Hello World");
});
var options = {};
var server = http.createServer(options, app);
server.listen(3000, function(){
console.log("Start listening on port 3000.")
});
I am trying to send a request to this server via the following html page (which I am opening with Safari):
<!DOCTYPE html>
<html>
<head>
<script>
function request(){
var req = new XMLHttpRequest();
req.open('GET', 'http://localhost:3000');
req.onreadystatechange = function(){
alert(req.status);
if(req.status === 200){
alert(req.responseText);
}
}
req.send();
}
</script>
</head>
<body>
<button onclick="request()">Click</button>
</body>
</html>
My problem is, that the server does not seem to respond to the request. The status code alert says that I receive status code '0'. Is this a problem due to the fact, that the client page is not hosted through a webserver?