不能在ExpressJS服务索引文件

时间:2016-09-05 13:35:54

标签: javascript html node.js

我正在构建一个Web应用程序,其中包含多个页面。然而,这些页面中的每一个都具有多个视图。最初的设计是将每个页面设置为SPA,并从Node.js后端提供index.html,并让React.js处理所有视图路由。

事实证明这比我想象的要复杂得多。这是问题所在。

Node.js路由(摘录):

router.get("/", function(request, response){
    response.sendFile("/index.html", {
        root: __dirname + "/../client/pages/home/"
    });
});

然后在index.html(摘录)中:

<html>
<head>
    <title>New Website</title>
</head>
<body>

    <h1>Welcome to my website..</h1>

    <div id="App">
    </div>

    <script src="index.js"></script>
</body>
</html>

index.js未包括在内。当浏览器尝试包含index.js时,它正在从Node.js服务器请求路由www.example.com/index.js,这显然没有设置,所以我收到Cannot GET /index.js响应。

每个SPA都在index.htmlindex.js的各个文件夹中。目前,不能创建包含所有js文件的公用文件夹。

=============================================== ===========================

已解决:这是我使用的代码

clientController.js

module.exports = function(app){
    router.get("/", function(request, response){
        app.use("/", express.static(__dirname + "/../client/pages/page1/"));
        response.sendFile("index.html", {
            root: __dirname + "/../client/pages/page1/"
        });
    });

    router.get("/page2/*", function(request, response){
        app.use("/", express.static(__dirname + "/../client/pages/page2/"));
        response.sendFile("index.html", {
            root: __dirname + "/../client/pages/page2/"
        });
    });

    return router;
};

server.js

var routes = require("controllers/clientController.js");
app.use("/", routes);

接受的答案,我必须在sendFile()行添加以防止请求挂起。这样可以确定响应并允许在HTML文件中包含文件包含的正确路径。

2 个答案:

答案 0 :(得分:2)

ExpressJs documentation中所述,您必须使用express.static(root, [options])来创建管理您网站的中间件。这是Express中唯一的内置中间件功能。

Express app示例:

在你必须初始化表达app之前:

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

然后开始添加中间件:

注意:您可以添加多个中间件,并按顺序链接它们。换句话说,您可以在网站中间件之前添加标头处理程序。

Headers中间件处理程序示例:

app.use(function (req, res, next) {
    /* you can also handle https redirection */
    // if (req.protocol != "https") {
    //     res.writeHead(301, {"Location": "https://" + req.headers['host'] + req.url});
    //     res.end();
    //     return;
    // }

    // here you can config you response headers
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');


    next(); // this will pass the request to the next middleware  
});

网站路由器示例:

要使用ExpressJs创建中间件网站,您必须将express.static与您的相关网站文件夹一起使用,并将您的默认页面命名为index.html

app.use("/", express.static('PATH_TO_WEBSITE FOLDER'));

Full ExpressJs应用程序示例:

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

app.use(function (req, res, next) {
    /* you can also handle https redirection */
    // if (req.protocol != "https") {
    //     res.writeHead(301, {"Location": "https://" + req.headers['host'] + req.url});
    //     res.end();
    //     return;
    // }

    // here you can config you response headers
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');


    next(); // this will pass the request to the next middleware  
});


app.use("/", express.static('PATH_TO_WEBSITE FOLDER'));


var httpServer = http.createServer(app);
httpServer.listen(80, "HERE_PUT_YOUR_DOMAIN", function () {
    console.log('HTTP: express-example running on port ' + 80 + '.');
});

答案 1 :(得分:0)

您的目录将被视为静态。您不需要创建单独的目录或定义单独的路径。

router.use(express.static( __dirname + "/../client/pages/home/"));
相关问题