我试图找出如何加载和呈现一个基本的HTML文件,这样我就不必写代码:

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

当前回答

这是一个相当老的问题……但如果你在这里的用例是简单地向浏览器发送一个特定的HTML页面,我会使用像这样简单的东西:

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

var server = http.createServer(function(req, res){
  var stream = fs.createReadStream('test.html');
  stream.pipe(res);
});
server.listen(7000);

其他回答

这可能会更好一些,因为您将流式文件而不是像fs.readFile那样将其全部加载到内存中。

var http = require('http');
var fs = require('fs');
var path = require('path');
var ext = /[\w\d_-]+\.[\w\d]+$/;

http.createServer(function(req, res){
    if (req.url === '/') {
        res.writeHead(200, {'Content-Type': 'text/html'});
        fs.createReadStream('index.html').pipe(res);
    } else if (ext.test(req.url)) {
        fs.exists(path.join(__dirname, req.url), function (exists) {
            if (exists) {
                res.writeHead(200, {'Content-Type': 'text/html'});
                fs.createReadStream('index.html').pipe(res);
            } else {
                res.writeHead(404, {'Content-Type': 'text/html'});
                fs.createReadStream('404.html').pipe(res);
        });
    } else {
        //  add a RESTful service
    }
}).listen(8000);

如果你使用管道,它就非常简单。下面是server.js的代码片段。

Var HTTP = require(' HTTP '); Var fs = require('fs'); 函数onRequest(req, res){ 日志("用户提出请求。“+ req.url); res.writeHead(200, {'Content-Type': 'text/html'}); var readStream = fs. varcreatererestream (__dirname + '/index.html','utf8'); /*包含你的HTML文件和目录名,而不是<<__dirname + '/index.html'>>*/ readStream.pipe (res); } http.createServer (onRequest) .listen (7000); console.log('Web服务器正在运行…');

试试这个:

var http = require('http');
var fs = require('fs');
var PORT = 8080;

http.createServer((req, res) => {
    fs.readFile('./' + req.url, (err, data) => {
        if (!err) {
            var dotoffset = req.url.lastIndexOf('.');
            var mimetype = dotoffset == -1 ? 'text/plaint' : {
                '.html': 'text/html',
                '.css': 'text/css',
                '.js': 'text/javascript',
                '.jpg': 'image/jpeg',
                '.png': 'image/png',
                '.ico': 'image/x-icon',
                '.gif': 'image/gif'
            }[ req.url.substr(dotoffset) ];
            res.setHeader('Content-Type', mimetype);
            res.end(data);
            console.log(req.url, mimetype);
        } else {
            console.log('File not fount: ' + req.url);
            res.writeHead(404, "Not Found");
            res.end();
        }
    });
 }).listen(PORT);

有了这个,你可以包括js, css源代码时链接他们,你可以加载图标,imgs, gif。如果你愿意,你可以添加更多。

用ejs代替jade

npm 安装 EJS

app.js

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

/线路/ index.js

exports.index = function(req, res){
res.render('index', { title: 'ejs' });};

我学到的最好的方法是在html文件中使用express,因为express有很多优势。如果你想的话,你也可以把它扩展到Heroku平台上。

var express = require("express");
var app     = express();
var path    = require("path");


app.get('/',function(req,res){
  res.sendFile(path.join(__dirname+'/index.html'));
});

app.listen(3000);



console.log("Running at Port 3000");

干净而且最好!!