使用 express.js 在 node.js 中提供 html 的最佳实践是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16593686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:26:47  来源:igfitidea点击:

What is the best practice for serving html in node.js with express.js?

htmlnode.jsexpressstatic-files

提问by Alberto Zaccagni

I currently am serving all my html right in my app.js/server.js file like this:

我目前正在我的 app.js/server.js 文件中提供我所有的 html,如下所示:

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

I imagine if I have 15+ html pages that this is probably not the best way to call them. Is there a better way to serve them from another file or location and using export or something to be able to call just one function or something on app.js. It might be what the routing is for but maybe I do not understand it too well.

我想如果我有 15 个以上的 html 页面,这可能不是调用它们的最佳方式。有没有更好的方法可以从另一个文件或位置为它们提供服务,并使用导出或其他东西来调用 app.js 上的一个函数或其他东西。这可能是路由的用途,但也许我不太了解它。

(added more code that is in the same file)

(在同一文件中添加了更多代码)

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/public');

// used below code to render html files
app.engine('html', require('ejs').renderFile);

app.set('view engine', 'ejs');
app.use(express.favicon("public/img/favicon.ico"));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

回答by Alberto Zaccagni

You could use the staticmiddleware:

您可以使用static中间件:

app.use("/", express.static(__dirname));

A server example:

服务器示例:

var express = require('express');
var app = express();
app.use('/', express.static(__dirname + '/public'));
app.listen(3000, function() { console.log('listening')});

This is the file structure:

这是文件结构:

.
├── public
│?? ├── a.html
│?? ├── b.html
│?? └── c.html
└── server.js

Documentation:

文档:

回答by Jacob Gillespie

One idea would be to use a catch-all kind of route as the last route, like the following:

一个想法是使用一种包罗万象的路线作为最后一条路线,如下所示:

app.get('/:page', function(req, res) {
  res.sendfile(path.join(__dirname, 'public', 'pages', path.basename(req.params.page) + '.html'));
});

That would require that you put your .html files into public/pages/about.html, etc.

这将要求您将 .html 文件放入 public/pages/about.html 等。

You might want to switch the order of the static file router so that static files get precedence over routes, too, unless you want that route catching things in the public folder, like this:

您可能希望切换静态文件路由器的顺序,以便静态文件也优先于路由,除非您希望该路由捕获公共文件夹中的内容,如下所示:

app

应用程序

app.set('view engine', 'ejs');
app.use(express.favicon("public/img/favicon.ico"));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);