2013-08-02 9 views
6

के साथ स्थानांतरित किया गया पहला: मैं एक्सप्रेस का उपयोग नहीं कर रहा हूं।Node.js - संसाधन स्क्रिप्ट के रूप में व्याख्या किया गया है लेकिन एमआईएमई प्रकार टेक्स्ट/सादा

इस तरह से, जब मैं अपना index.html लोड करता हूं तो यह readFile प्रत्येक सीएसएस और जेएस पृष्ठों जैसे संलग्न फाइलों को दोबारा दर्ज करता है। लेकिन यह हमेशा मेरे निरीक्षक (क्रोम) में इस त्रुटि देता है:

संसाधन स्क्रिप्ट के रूप में व्याख्या लेकिन MIME प्रकार के पाठ के साथ स्थानांतरित/सादे

मैं बिल्कुल पता नहीं क्यों यह यह कर रहा है। यहां मेरा कोड है:

var http = require('http'); 
var querystring = require('querystring'); 
var fs = require('fs'); 
var url = require('url'); 

function route(handle, pathname, response, request){ 
console.log("About to route a request for " + pathname); 

if (typeof handle[pathname] === "function"){   
    handle[pathname](response, request); 
    } else { 

    var file_path = ""; 

    // parses the url request for a file and pulls the pathname 
    var url_request = url.parse(request.url).pathname;  
    var tmp = url_request.lastIndexOf("."); 
    var extension = url_request.substring((tmp + 1)); 

    file_path = url_request.replace("/", ""); 

    //load needed pages and static files 
    fs.readFile(file_path, function (error, contents){ 
     if(error){ 
      console.log('DIE!'); 
      console.log(error); 
      response.writeHeader(500, {"Content-Type": "text/html"}); 
      response.end("<h1>FS READ FILE ERROR: Internal Server Error!</h1>");  
     } 
     else{ 
      console.log('SUCCESS!'); 
      // set content type 
      if (extension === 'html') response.writeHeader(200, {"Content-Type": 'text/html'}); 
      else if (extension === 'htm') response.writeHeader(200, {"Content-Type": 'text/html'}); 
      else if (extension === 'css') response.writeHeader(200, {"Content-Type": 'text/css'}); 
      else if (extension === 'js') response.writeHeader(200, {"Content-Type": 'text/javascript'}); 
      else if (extension === 'png') response.writeHeader(200, {"Content-Type": 'image/png'}); 
      else if (extension === 'jpg') response.writeHeader(200, {"Content-Type": 'image/jpg'}); 
      else if (extension === 'jpeg') response.writeHeader(200, {"Content-Type": 'image/jpeg'}); 
      else { console.log("NO CORRECT EXTENSION")}; 
      console.log(extension); 
      response.end(contents); 
     } 

     response.end(); 
    }); 
    response.end(); 
} 
} 

exports.route = route; 

मैं इसे कैसे ठीक कर सकता हूं? इसने मेरे प्रोजेक्ट को अपने ट्रैक में रोक दिया है।

नोट: Node.js incorrect path problems

+0

सबसे पहले, यह 'writeHead', नहीं' writeHeader' है। अगला - यिक्स, वह 'अगर'। आपको एक वस्तु का उपयोग करना चाहिए। 'response.writeHead (200, {'सामग्री-प्रकार': माइम टाइप्स [एक्सटेंशन]});'। – Ryan

+0

'writeHead' पर अच्छा पकड़। लेकिन माइम के रूप में, http://stackoverflow.com/questions/18004512/node-js-incorrect-path-problems/18004553?noredirect=1#comment26330812_18004553 पर मेरा पिछला प्रश्न देखें, यह मूल रूप से था। इससे कोई फर्क नहीं पड़ता। यह मेरा दूसरा प्रमुख पुनरावृत्ति है। – JDillon522

+0

क्या आपने सीधे स्क्रिप्ट यूआरएल पर जाने का प्रयास किया है? क्या यह काम करता है? – Ryan

उत्तर

7

कोशिश है कि पिछले response.end बाहर टिप्पणी: इस पहले के एक समस्या मैं यहाँ के बारे में बात की थी की एक प्रगति है। ऐसा मत सोचो कि आपको इसकी ज़रूरत है।

response.end(); 
    }); 
    // response.end(); 
    } 
    exports.route = route; 

मुझे लगता है response.end कहा जाता रहा है कि ठीक पहले fs.readfile कॉलबैक अंदर एक निष्पादित करने के लिए, हो सकता है दे रही सही हेडर "सामग्री प्रकार" नहीं सर्वर पर हैडर को बंद बंद करने का समय है: 'पाठ/जावास्क्रिप्ट' एक मौका भेजा पाने ..

अपने कोड पर यह कोशिश की, मेरे लिए काम किया ..

+2

यूप, वह था। एफएमएल मैं छह घंटे से अधिक समय तक इस पर घूर रहा हूं। धन्यवाद आदमी – JDillon522

+0

धन्यवाद दोस्तों। इससे मुझे मदद मिलती है –

संबंधित मुद्दे

 संबंधित मुद्दे