-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (45 loc) · 1.38 KB
/
server.js
File metadata and controls
58 lines (45 loc) · 1.38 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//Lets require/import the HTTP module
var http = require('http');
var dispatcher = require('httpdispatcher');
//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){
try {
//log the request on console
console.log(request.url);
//Disptach
dispatcher.dispatch(request, response);
} catch(err) {
console.log(err);
}
}
//For all your static (js/css/images/etc.) set the directory name (relative path).
dispatcher.setStatic('resources');
//A sample GET request
//A sample POST request
dispatcher.onPost("/login", function(req, res) {
setTimeout(() => {
if(req.body)
{
console.log(req.body);
var data = JSON.parse(req.body);
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
});
if(data.username === "User" && data.password === "Password") {
res.end(JSON.stringify({ Auth: "Logged", Language: "EN" }));
} else {
res.end(JSON.stringify({ Auth: "Denied" }));
}
}
},3000);
});
//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);
});