无法在 ExpressJS 服务的索引文件中包含 JS 文件 [英] Cannot include JS files in ExpressJS served index file

查看:29
本文介绍了无法在 ExpressJS 服务的索引文件中包含 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(代码段)中:

<头><title>新网站</title><身体><h1>欢迎来到我的网站..</h1><div id="应用程序">

<script src="index.js"></script></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/"});});返回路由器;};

server.js

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

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

解决方案

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

router.use(express.static( __dirname + "/../client/pages/home/"));

I'm building a web application at the moment, which has multiple pages incorporated. Each of these pages, however, have multiple views. The original design was to set each of the pages up as an SPA and just serve the index.html from the Node.js back-end and let React.js handle all the view routing.

This has proven to be more complex than I thought. Here is the issue.

The Node.js route (snippet):

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

But then in index.html (snippet):

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

The index.js is not being included. When the browser tries to include index.js it's requesting the route www.example.com/index.js from the Node.js server which is obviously not set up so I'm getting a Cannot GET /index.js response.

Each of the SPAs are in their individual folders with index.html and index.js. Creating a public folder with all the js files is not an option at the moment.

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

SOLVED: THIS IS THE CODE I USED

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

The accepted answer, I had to add in the sendFile() line as well to prevent the request from hanging. This settles the response and allows for the correct paths for file includes in the HTML files.

解决方案

Your directory will be treated as static. You don't need to make a separate directory or define a separate route.

router.use(express.static( __dirname + "/../client/pages/home/"));

这篇关于无法在 ExpressJS 服务的索引文件中包含 JS 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆