Created
          January 18, 2013 12:37 
        
      - 
      
 - 
        
Save joao-parana/4564314 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var http = require('http'); | |
| var path = require('path'); | |
| var fs = require('fs'); | |
| var mimeTypes = { | |
| '.js' : 'text/javascript', | |
| '.html' : 'text/html', | |
| '.css' : 'text/css' | |
| }; | |
| var processRequest = function(request, response) { | |
| console.log('Inicio'); | |
| var lookup = path.basename(decodeURI(request.url)) || 'index.html', f = 'content/' | |
| + lookup; | |
| console.log('log: ', f); | |
| fs.exists(f, function(exists) { // path.exists for Node 0.6 and below | |
| if (exists) { | |
| fs.readFile(f, function(err, data) { | |
| if (err) { | |
| response.writeHead(500); | |
| response.end('Server Error!'); | |
| return; | |
| } | |
| var headers = { | |
| 'Content-type' : mimeTypes[path.extname(lookup)] | |
| }; | |
| response.writeHead(200, headers); | |
| response.end(data); | |
| console.log('Arquivo lido: ', f); | |
| }); | |
| return; | |
| } | |
| response.writeHead(404); // no such file found! | |
| response.end('Page Not Found!'); | |
| }); | |
| }; | |
| http.createServer(processRequest).listen(80); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment