-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-server.js
34 lines (26 loc) · 1.11 KB
/
basic-server.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
/*
*building an api
text a fetch call, ajax call
get data to the webpage
respond to http request and send some back
* */
//bring a node package that build our web server
const http = require('http');
//takes two parameters a function request and response
const my_server = http.createServer(function(req,res){
//test for downloading data setting the content type
res.setHeader('Content-type', 'application/json');
//set it equal to anything aka * to allow data to go anywhere
res.setHeader('Access-Control-Allow-Origin', "*");
//setHeader can be called more than one time but writeHead is always last and called only once and pass in the status code 200 for success
res.writeHead(200);
let dataObject = {"id": 63526, "name": "NelanIsACLover", "email": "[email protected]"};
//dataObject must be a string for us to send back
let data = JSON.stringify(dataObject);
//send everything back to the browser because everything is done
res.end(data);
});
//pass in the port number and a callback func that will do sth
my_server.listen(63526, function(){
console.log("Listening on port 63525");
});