在Node.js中加载基本的HTML [英] Loading basic HTML in Node.js

查看:150
本文介绍了在Node.js中加载基本的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里只是一个简单的Node.js noob问题。我试图找出如何加载和显示一个基本的HTML文件,所以我不必编写像 response.write('...< p> blahblahblah< / p> ; ...');

Just a simple Node.js noob question here. I'm trying to find out how to load and "display" a basic HTML file so I don't have to write code like response.write('...<p>blahblahblah</p>...');.

推荐答案

>使用 fs图书馆的方式。我不确定它是否干净。

I just found one way using the fs library. I'm not certain if it's the cleanest though.

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);
});

基本概念是原始文件读取和转储内容。尽管如此,仍然开放给清洁选项!

The basic concept is just raw file reading and dumping the contents. Still open to cleaner options, though!

这篇关于在Node.js中加载基本的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆