Render basic HTML view?

2019-01-01 05:57发布

I have a basic node.js app that I am trying to get off the ground using Express framework. I have a views folder where I have an index.html file. But I receive the following error when loading the web browser.

Error: Cannot find module 'html'

Below is my code.

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')

What am I missing here?

26条回答
听够珍惜
2楼-- · 2019-01-01 06:50

You can have jade include a plain HTML page:

in views/index.jade

include plain.html

in views/plain.html

<!DOCTYPE html>
...

and app.js can still just render jade:

res.render(index)
查看更多
大哥的爱人
3楼-- · 2019-01-01 06:50

If you're using express@~3.0.0 change the line below from your example:

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

to something like this:

app.set("view options", {layout: false});
app.use(express.static(__dirname + '/public'));

I made it as described on express api page and it works like charm. With that setup you don't have to write additional code so it becomes easy enough to use for your micro production or testing.

Full code listed below:

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

app.set("view options", {layout: false});
app.use(express.static(__dirname + '/public'));

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

app.listen(8080, '127.0.0.1')
查看更多
流年柔荑漫光年
4楼-- · 2019-01-01 06:52

You could also read the html file and send it.

app.get('/', function(req, res) {
    fs.readFile(__dirname + '/public/index.html', 'utf8', function(err, text){
        res.send(text);
    });
});
查看更多
闭嘴吧你
5楼-- · 2019-01-01 06:52

Add the following Lines to your code

  1. Replace "jade" with "ejs" & "X.Y.Z"(version) with "*" in package.json file

      "dependencies": {
       "ejs": "*"
      }
    
  2. Then in your app.js File Add following Code :

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

    app.set('view engine', 'html');

  3. And Remember Keep All .HTML files in views Folder

Cheers :)

查看更多
荒废的爱情
6楼-- · 2019-01-01 06:58

I usually use this

app.configure(function() {
    app.use(express.static(__dirname + '/web'));
});

Just be careful because that'll share anything in the /web directory.

I hope it helps

查看更多
明月照影归
7楼-- · 2019-01-01 06:59

From the Express.js Guide: View Rendering

View filenames take the form Express.ENGINE, where ENGINE is the name of the module that will be required. For example the view layout.ejs will tell the view system to require('ejs'), the module being loaded must export the method exports.render(str, options) to comply with Express, however app.register() can be used to map engines to file extensions, so that for example foo.html can be rendered by jade.

So either you create your own simple renderer or you just use jade:

 app.register('.html', require('jade'));

More about app.register.

Note that in Express 3, this method is renamed app.engine

查看更多
登录 后发表回答