Skip to content

Instantly share code, notes, and snippets.

@Gishi1
Forked from omkz/server.js
Created April 10, 2017 17:47
Show Gist options
  • Save Gishi1/3b9b78cbd4bf1227192262987d5edd43 to your computer and use it in GitHub Desktop.
Save Gishi1/3b9b78cbd4bf1227192262987d5edd43 to your computer and use it in GitHub Desktop.
node.js - routing aplikasi node.js
var http = require("http");
var url = require('url');
var fs = require('fs');
var server = http.createServer(function(request, response){
console.log('Connection');
var path = url.parse(request.url).pathname;
switch(path){
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world');
break;
case 'socket.html':
fs.readFile(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("opps this doesn't exist - 404");
}
else{
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
}
});
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404");
break;
}
response.end();
});
server.listen(8001);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment