我有一个基本的Node.js应用程序,我试图使用Express框架启动。我有一个views文件夹,其中有一个index.html文件。但是我在加载网页时收到以下错误:

Error: Cannot find module 'html'

下面是我的代码。

var express = require('express');
var app = express.createServer();

app.use(express.staticProvider(__dirname + '/public'));

app.get('/', function(req, res) {
    res.render('index.html');
});

app.listen(8080, '127.0.0.1')

我错过了什么?


当前回答

试试这个。这对我很管用。

app.configure(function(){

  .....

  // disable layout
  app.set("view options", {layout: false});

  // make a custom html template
  app.register('.html', {
    compile: function(str, options){
      return function(locals){
        return str;
      };
    }
  });
});

....

app.get('/', function(req, res){
  res.render("index.html");
});

其他回答

对于纯html,你不需要任何npm包或中间件

就用这个吧:

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

如果您不需要使用views目录,只需将html文件移动到下面的公共目录即可。

然后,将这一行添加到app.configure中,而不是'/views'。

server.use(express.static(__dirname + '/public'));

我在特快3中也遇到了同样的问题。X和节点0.6.16。以上给出的解决方案不适用于最新版本的express 3.x。他们删除了app.register方法,并添加了app.engine方法。如果您尝试上述解决方案,可能会出现以下错误。

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'register'
    at Function.<anonymous> (/home/user1/ArunKumar/firstExpress/app.js:37:5)
    at Function.configure (/home/user1/ArunKumar/firstExpress/node_modules/express/lib/application.js:399:61)
    at Object.<anonymous> (/home/user1/ArunKumar/firstExpress/app.js:22:5)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)
    at EventEmitter._tickCallback (node.js:192:40)

来消除错误消息。在app.configure函数中添加以下代码行

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

注意:你必须安装ejs模板引擎

npm install -g ejs

例子:

app.configure(function(){

  .....

  // disable layout
  app.set("view options", {layout: false});

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

....

app.get('/', function(req, res){
  res.render("index.html");
});

注意:最简单的解决方案是使用ejs模板作为视图引擎。你可以在*中编写原始HTML。Ejs视图文件。

这里是一个完整的文件演示的express服务器!

https://gist.github.com/xgqfrms-GitHub/7697d5975bdffe8d474ac19ef906e906

希望对你有所帮助!

// simple express server for HTML pages! // ES6 style const express = require('express'); const fs = require('fs'); const hostname = '127.0.0.1'; const port = 3000; const app = express(); let cache = [];// Array is OK! cache[0] = fs.readFileSync( __dirname + '/index.html'); cache[1] = fs.readFileSync( __dirname + '/views/testview.html'); app.get('/', (req, res) => { res.setHeader('Content-Type', 'text/html'); res.send( cache[0] ); }); app.get('/test', (req, res) => { res.setHeader('Content-Type', 'text/html'); res.send( cache[1] ); }); app.listen(port, () => { console.log(` Server is running at http://${hostname}:${port}/ Server hostname ${hostname} is listening on port ${port}! `); });

如果你试图提供一个HTML文件,其中已经有它所有的内容在里面,那么它不需要被“渲染”,它只需要被“服务”。呈现是指在页面发送到浏览器之前让服务器更新或注入内容,并且它需要额外的依赖项,如ejs,如其他答案所示。

如果你只是想让浏览器根据他们的请求指向一个文件,你应该像这样使用res.sendFile():

const express = require('express');
const app = express();
var port = process.env.PORT || 3000; //Whichever port you want to run on
app.use(express.static('./folder_with_html')); //This ensures local references to cs and js files work

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/folder_with_html/index.html');
});

app.listen(port, () => console.log("lifted app; listening on port " + port));

这样,除了express,您就不需要其他依赖项了。如果你只是想让服务器发送你已经创建的html文件,上面是一种非常轻量级的方式。