node.js 让 Express 提供默认页面的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21119288/
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-09-02 16:39:14  来源:igfitidea点击:

simplest way to have Express serve a default page?

node.jsexpress

提问by StevenV

I'm using this to have Node.js/Express setup as a rudimentary web server - it just serves a set of static pages without any other processing. I'd like it to always serve /default.html when a browser fetches the site without any filename.

我正在使用它来将 Node.js/Express 设置为基本的 Web 服务器 - 它只提供一组静态页面,没有任何其他处理。当浏览器在没有任何文件名的情况下获取站点时,我希望它始终提供 /default.html 。

var express = require("express");
var app = express();
var port = process.env.PORT || 5000;

app.configure(function(){
  app.use(express.bodyParser());
});

app.use(express.logger());

app.use(express.static(__dirname ));

app.listen(port, function() {
  console.log("Listening on " + port);
});

I've tried using res.sendfile and res.redirect, but without much success; I'm obviously missing something as I end up with a 'has no method' error.

我试过使用 res.sendfile 和 res.redirect,但没有取得多大成功;我显然错过了一些东西,因为我最终遇到了“没有方法”的错误。

What would you say is the simplest way of achieving my goal?

你认为实现我的目标最简单的方法是什么?

采纳答案by Alex Curtis

You could do something like this. Assuming its an html file that is relative to the .js file:

你可以做这样的事情。假设它是一个相对于 .js 文件的 html 文件:

app.get('/', function(req, res){
    res.sendfile('default.html', { root: __dirname + "/relative_path_of_file" } );
});

回答by Ryan Ruppert

I looked briefly for the best practices for serving a public folder and specifying the default page to serve. After reviewing the 'Express middleware' documentation, my solution looks like the following,

我简要地查找了提供公共文件夹和指定要提供的默认页面的最佳实践。查看“Express中间件”文档后,我的解决方案如下所示,

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

var options = {
  index: "coming-soon.html"
};

app.use('/', express.static('app', options));

var server = app.listen(8081, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('my app is listening at http://%s:%s', host, port);
});

回答by mohRamadan

It's weird that no one mentioned redirection.

奇怪的是没有人提到重定向。

app.get('/', function(req, res){
    res.redirect('/default.html');
});

This -of course- assumes that you have your 'default.html' at the public path. You can set your public path by using:

这当然假设您在公共路径上有“default.html”。您可以使用以下方法设置公共路径:

app.use(express.static(relative_path_to_project_root));

回答by Belden Schroeder

I've added my own variation with some extra "meat" on it to demonstrate how you can even include routing. I put all the following code in a file called "routes.js":

我已经添加了我自己的变体,上面有一些额外的“肉”,以演示如何甚至可以包含路由。我将以下所有代码放在一个名为“routes.js”的文件中:

var express = require('express'),
    painting = require('./controllers').Painting,
    gallery = require('./controllers').Gallery;

module.exports.initialize = function(appSvr, router) {
    router.get('/paintings', painting.list);
    router.get('/galleries', gallery.list);

    appSvr.use('/', router);
    appSvr.use('/', express.static(__dirname + '/../public', { index: 'index.html' }));
};

I then call this file in my "configure.js" file that will be required in my "server.js" file:

然后我在我的“server.js”文件中需要的“configure.js”文件中调用这个文件:

var routes = require('./routes'),
    express = require('express'),
    bodyParser = require('body-parser');

module.exports = function(appSvr) {
    appSvr.use(bodyParser.urlencoded());
    appSvr.use(bodyParser.json());
    routes.initialize(appSvr, new express.Router());

    return appSvr;
};

I haven't seen how best to handle routing, but this code seems to work for me.

我还没有看到如何最好地处理路由,但这段代码似乎对我有用。

Would love some feedback!

希望得到一些反馈!