-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
37 lines (30 loc) · 927 Bytes
/
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
34
35
36
37
//dependencies
const http = require('http'),
url = require('url'),
formidable = require('formidable');
//change port here
const PORT = 5000;
//starting server
function start(handle,route)
{
http.createServer((req,res) => {
//get the path
var urlParsed = url.parse(req.url);
console.log("Request received :",urlParsed.pathname);
//pass router function the pathname
//example for dataobj{
// data : "this is data",
// type : "application/json",
// HTTPcode : 404
// }
route(handle,urlParsed,(dataobj,req)=>{
//use response object here
res.writeHead(dataobj.HTTPcode,{"content-type":dataobj.type});
res.write(JSON.stringify(dataobj.data));
res.end();
},req);
}).listen(PORT,()=>{
console.log("Server listening on port :",PORT);
});
}
exports.start = start;