github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/docs/examples/example-nodejs-app/server.js (about) 1 //Lets require/import the HTTP module 2 var http = require('http'); 3 var dispatcher = require('httpdispatcher'); 4 5 //Lets define a port we want to listen to 6 const PORT=8080; 7 8 //We need a function which handles requests and send response 9 function handleRequest(request, response){ 10 try { 11 //log the request on console 12 console.log(request.url); 13 //Disptach 14 dispatcher.dispatch(request, response); 15 } catch(err) { 16 console.log(err); 17 } 18 } 19 20 //Create a server 21 var server = http.createServer(handleRequest); 22 23 //Lets start our server 24 server.listen(PORT, function(){ 25 //Callback triggered when server is successfully listening. Hurray! 26 console.log("Server listening on: http://localhost:%s", PORT); 27 }); 28 29 //For all your static (js/css/images/etc.) set the directory name (relative path). 30 dispatcher.setStatic('resources'); 31 32 //A sample GET request 33 dispatcher.onGet("/page1", function(req, res) { 34 res.writeHead(200, {'Content-Type': 'text/plain'}); 35 res.end('Page One'); 36 }); 37 38 //A sample POST request 39 dispatcher.onPost("/post1", function(req, res) { 40 res.writeHead(200, {'Content-Type': 'text/plain'}); 41 res.end('Got Post Data'); 42 });