2017-11-14 16 views
0

私はHerokuを使用してWebサイトを展開しています.node.jsを使用しています私のindex.jsファイルでは、チュートリアルではresponse.endで "Hello World"を使用しています。しかし、代わりに私のindex.htmlファイルを表示したい。どうすればいい?Herokuをindex.htmlにルーティングしようとしています

var http = require('http') 
 

 
http.createServer(function (request, response){ 
 
    response.writeHead(200, {"Content-Type": "text/plain"}) 
 
    response.end("Hello World/n") 
 
}).listen(process.env.PORT)

+0

response.end( "hello world")をresponse.sendFile( 'index.html')に変更します。 – chenkehxx

答えて

1

あなたはこのようにそれを行うことができます。
fs.readFile HTMLファイルを読み込み、バッファに変換します。 response.writeHeadは応答ヘッダーを定義し、コンテンツタイプをhtmlとして指定します。 response.endがファイルを送信し、応答を完了します。

var http = require('http') 
var fs = require('fs') 

http.createServer(function (request, response) { 
    fs.readFile('index.html', function (err, file) { 
    response.writeHead(200, { "Content-Type": "text/html" }) 
    response.end(file) 
    }) 
}) 
    .listen(process.env.PORT) 
関連する問題