github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/test/test_apps/test_nodejs_app/test_nodejs_app.js (about) 1 //Lets require/import the HTTP module 2 var http = require('http'); 3 var fs = require('fs'); 4 var dispatcher = require('httpdispatcher'); 5 6 //Lets define a port we want to listen to 7 const PORT=8080; 8 9 //We need a function which handles requests and send response 10 function handleRequest(request, response){ 11 try { 12 //log the request on console 13 console.log(request.url); 14 //Disptach 15 dispatcher.dispatch(request, response); 16 } catch(err) { 17 console.log(err); 18 } 19 } 20 21 //Create a server 22 var server = http.createServer(handleRequest); 23 24 //Lets start our server 25 server.listen(PORT, function(){ 26 //Callback triggered when server is successfully listening. Hurray! 27 console.log("Server listening on: http://localhost:%s", PORT); 28 }); 29 30 //ping test 31 dispatcher.onGet("/ping_test", function(req, res) { 32 var responseObject = {message: "pong"}; 33 res.end(JSON.stringify(responseObject)); 34 }); 35 36 //env test 37 dispatcher.onGet("/env_test", function(req, res) { 38 var responseObject = {message: process.env.KEY}; 39 res.end(JSON.stringify(responseObject)); 40 }); 41 42 //mount_test test 43 dispatcher.onGet("/mount_test", function(req, res) { 44 fs.readFile('/data/data.txt', 'utf8', function (err,data) { 45 if (err) { 46 console.log(err); 47 res.end(err.toString('utf8')); 48 return 49 } 50 var responseObject = {message: data.toString('utf8')}; 51 res.end(JSON.stringify(responseObject)); 52 }); 53 });