2016-06-10 202 views
0

如何讓我的css文件被節點js讀取。我有下面的代碼閱讀的HTML文件。從nodejs讀取css文件

function displayForm(res) { 
fs.readFile('index.html', function (err, data) { 
    res.writeHead(200, { 
     'Content-Type': 'text/html', 
    }); 
    res.write(data); 
    res.end(); 
}); 
} 

回答

1

你可以做

HTML

fs.readFile(__dirname + '/public/index.html', function (err, data) { 
    if (err) console.log(err); 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.write(data); 
    res.end(); 
}); 

JS用同樣的方法

fs.readFile(__dirname + '/public/js/script.js', function (err, data) { 
    if (err) console.log(err); 
    res.writeHead(200, {'Content-Type': 'text/javascript'}); 
    res.write(data); 
    res.end(); 
}); 

CSS

fs.readFile(__dirname + '/public/css/style.css', function (err, data) { 
    if (err) console.log(err); 
    res.writeHead(200, {'Content-Type': 'text/css'}); 
    res.write(data); 
    res.end(); 
});