我有一個簡單的html頁面,並且正在嘗試通過節點爲它服務。'content-type':'text/html'直到服務器死亡才被接受
當我將內容類型設置爲文本/純文本時,加載html文件的純文本。但是,當我將其更改爲「content-type」:「text/html」時,瀏覽器選項卡會連續加載而不更新。一旦我殺死節點應用程序,瀏覽器就會顯示該頁面,就好像它完全加載一樣。
Server代碼:
var http = require('http');
var query = require('querystring');
var fs = require('fs');
http.createServer(function (req, res) {
homeRoute(req,res)
}).listen(3000);
console.log('test');
function homeRoute(req, res) {
if (req.url === '/') {
var html = fs.readFileSync('./Index.html', {encoding: 'utf-8'});
res.writeHead(200, {"Content-Type": "text/html"});
console.log(html)
res.write(html);
res.end();
}
指數:
<DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Corporate Ipsum</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
<link rel="stylesheet" href="https://necolas.github.io/normalize.css/5.0.0/normalize.css">
<link rel="stylesheet" href="css/main.css"> </head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="src/app.js"></script>
<body>
<div class="container my-3">
<div class="col-sm-2">
<form method="post" action="/data" class="form-group">
<label for="sentenceCount">Sentences</label>
<input type="number" placeholder="10" name="sentencecount" id="sentenceCount" class="form-control parameters">
<button type="button" id="submit" class="btn btn-primary mt-1">Submit</button>
</form>
</div>
</div>
<div class="container mt-2" style="border:1px solid red">
</body> </html>
[Express.js接近響應(HTTP的可能重複: //www.stackoverflow.com/questions/13554319/express-js-close-response) – NineBerry
使用連接頭的另一種方法是正確添加內容長度頭。 – NineBerry
@NineBerry,謝謝你的迴應。我已經將連接關鍵字/值添加到writeHead,並嘗試添加內容長度,但我仍然遇到同樣的問題。 –