html - 在 Node.js 中加载基本 HTML

标签 html node.js

我正在尝试了解如何加载和呈现基本 HTML 文件,因此我不必编写如下代码:

response.write('...<p>blahblahblah</p>...');

最佳答案

我刚刚发现一种方法是使用fs library .我不确定它是否是最干净的。

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


fs.readFile('./index.html', function (err, html) {
    if (err) {
        throw err; 
    }       
    http.createServer(function(request, response) {  
        response.writeHeader(200, {"Content-Type": "text/html"});  
        response.write(html);  
        response.end();  
    }).listen(8000);
});

基本概念只是原始文件读取和转储内容。不过,仍然有更清洁的选择!

关于html - 在 Node.js 中加载基本 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4720343/

相关文章:

javascript - 类型错误 : b is undefined when appending

html - 您可以使用具有 Flash 故障安全功能的 HTML5 视频/音频播放器吗?

Javascript/JQuery : Is there a way to append an image or text to an image that is downloaded?

javascript - 不在 <head> 标记中加载 jQuery 是否有负面影响?

css - 加载时页面跳转

javascript - 关于范围、node.js 和express 的问题

javascript - orientjs 的基本问题故障排除(node.js 的 OrientDB 驱动程序)

node.js - 使用 redis 的 NodeJS - 安装 hiredis 还是不安装?

node.js - 无法扩展接口(interface) 'http.IncomingMessage' 。你的意思是 'implements' 吗?

node.js - 我正在尝试将我的 Electron 应用程序转换为 windows portable