-
Notifications
You must be signed in to change notification settings - Fork 0
/
myFirstHTTPServer.js
35 lines (27 loc) · 1.29 KB
/
myFirstHTTPServer.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
32
33
34
35
//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=8080;
//We need a function which handles requests and send response
function handleRequest(request, response){
response.setHeader('Content-Type', 'application/json');
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('sample.json', 'utf8'));
// Website you wish to allow to connect
response.setHeader('Access-Control-Allow-Origin', 'file:///C:/_my_stuff/cluj_rental/google-maps.html');
// Request methods you wish to allow
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
response.setHeader('Access-Control-Allow-Credentials', true);
response.end(JSON.stringify(obj), null, 3);
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});