【问题标题】:'content-type': 'text/html' not accepted until server is killed'content-type': 'text/html' 在服务器被杀死之前不被接受
【发布时间】:2016-10-24 01:14:48
【问题描述】:

我有一个简单的 html 页面,并试图通过节点提供它。

当我将内容类型设置为 text/plain 时,会加载 html 文件的纯文本。但是,当我将其更改为 "content-type" : "text/html" 时,浏览器选项卡会持续加载而不更新。一旦我终止节点应用程序,浏览器就会显示页面,就好像它已完全加载一样。

服务器代码:

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 close response的可能重复
  • 使用连接头的另一种替代方法是正确添加内容长度头。
  • @NineBerry,感谢您的回复。我已将连接关闭键/值添加到 writeHead 并尝试添加内容长度,但我仍然遇到同样的问题。

标签: javascript node.js


【解决方案1】:

您需要添加Content-Length 标头,以便您的浏览器知道文件何时结束。你可以通过修改你的代码来做到这一点:

function homeRoute(req, res) {
    if (req.url === '/') {
        var html = fs.readFileSync('./Index.html'); // read this as a buffer
        res.writeHead(200, {"Content-Type": "text/html", "Content-Length": html.length}); // write the buffer's length
        console.log(html.toString('utf8')) // but when logging/writing explicitely say utf8
        res.write(html.toString('utf8')); // you can remove the toString here, but it adds a little readability 
        res.end();
    }

Node.js 文档在 res.writeHead 的底部有些提到这一点:https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers

【讨论】:

  • @PaulBDG,谢谢。我添加了内容长度字段,但仍然有同样的问题。知道为什么吗?
  • 你用的是什么浏览器?
  • Chrome 版本 53
  • 我正在运行您的原始代码,但没有发现任何问题。 Firefox 和 Chrome 立即加载它,我什至没有表达 Content-Length 标头。我想知道浏览器扩展是否可能会阻止它加载,或者您拥有的任何其他代码。
  • 它似乎正在加载 html 的标题而不是正文。我在 res.end 中添加了 console.log('ended') 并且我确实看到了它,所以它正在结束。我已禁用缓存、扩展等。如果有帮助,我已添加索引代码
猜你喜欢
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
相关资源
最近更新 更多