如何将index.html设置为其他页面,而不是主页

时间:2017-12-05 21:33:45

标签: html node.js indexing

我想在特殊页面'/ chess'上显示index.html,而不是在主页'/'上显示。

简单

app.get('/chess', function(req, res) {
 res.sendFile(__dirname + '/public/index.html');

});

不起作用。

我收到以下错误

enter image description here

3 个答案:

答案 0 :(得分:0)

使用下面的代码我能够重新映射

路由'/ chess'来提供index.html,

route'/'提供select.html。

const express = require("express");
const app = express();

app
    .get("/", (req, res)=>{
        res.sendFile(__dirname + "/public/select.html");
    })
    .get("/chess", (req, res)=>{
        res.sendFile(__dirname + "/public/index.html");
    });

app.listen(3000, () => console.log("Example app listening on port 3000!"));

您可以发布更多应用程序以查看是否还有其他问题吗?

答案 1 :(得分:0)

不要忘记使用use方法声明公共总监。

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

答案 2 :(得分:0)

创建更多游戏中心"尝试一种方法来实现一个导入到主app.js

的路由器

下面是我如何能够达到预期的结果

将app.js重命名为chess.js chess.js

var express = require('express');
var app = express();
app.use(express.static('public'));
app.use(express.static('dashboard'));
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;

... rest of code ...

http.listen(port, function() {
    console.log('listening on *: ' + port);
});

变为

module.exports = function(server){
    var express = require('express');
    var app = express.Router();
    app.use(express.static('public'));
    app.use(express.static('dashboard'));
    var io = require('socket.io')(server);

    ... rest of code ...

    return app;
}

删除第145-147行并添加module.exports = app;

创建一个新的app.js

const express = require("express");
const app = express();
const http = require('http').Server(app);
const chess = require("./chess")(http);
const port = process.env.PORT || 3000;

app
    .get("/", (req, res) => {
        res.sendFile(__dirname + "/public/selector.html");
    })
    .use("/", express.static("public"))
    .use("/chess",chess);

http.listen(port, function () {
    console.log('listening on *: ' + port);
});

这将在/ chess目录上创建国际象棋路由器,并允许您在/上安装selector.html。按照类似的模式,您可以安装其他游戏