-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.js
31 lines (30 loc) · 1.2 KB
/
http.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Include http module.
var http = require("http");
// Create the server. Function passed as parameter is called on every request made.
// request variable holds all request parameters
// response variable allows you to do anything with response sent to the client.
http.createServer(function (request, response) {
//this supposedly fixes things
request.on('readable', function () {
request.read();
});
// Attach listener on end event.
// This event is called when client sent all data and is waiting for response.
request.on("end", function () {
// Write headers to the response.
// 200 is HTTP status code (this one means success)
// Second parameter holds header fields in object
// We are sending plain text, so Content-Type should be text/plain
response.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Content-Type': 'text/plain'
});
console.log("Success! Connected to server.");
for(var i = 0; i < 10; i++)
response.write("Hello there\n");
response.end('Hello HTTP!');
});
// Listen on the 8080 port.
}).listen(8080);
console.log("waiting for connection ... ");