Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Express server未为my bundle.js提供服务_Node.js_Reactjs_Express - Fatal编程技术网

Node.js Express server未为my bundle.js提供服务

Node.js Express server未为my bundle.js提供服务,node.js,reactjs,express,Node.js,Reactjs,Express,我试图从我的React组件中提供bundle.js文件,但不确定为什么没有提供该文件。我正在使用Express,并且有一个包含以下文件的项目: project /dist bundle.js /views /home index.ejs /routes index.js app.js 在我的app.js中,我尝试为bundle.js提供服务: 我的简单视图/home/index.ejs如下所示: &

我试图从我的React组件中提供bundle.js文件,但不确定为什么没有提供该文件。我正在使用Express,并且有一个包含以下文件的项目:

project
    /dist
        bundle.js
    /views
       /home
          index.ejs
    /routes
       index.js
    app.js
在我的app.js中,我尝试为bundle.js提供服务:

我的简单视图/home/index.ejs如下所示:

<!DOCTYPE html>
<html>

<head>
  <title></title>
  <link rel='stylesheet' href='css/styles.css'/>
</head>

<body id="main-page">
    <div class="container">
    </div>
  <script src="bundle.js"></script>
</body>

</html>
这是我的app.js

var express = require("express");
    path = require("path"),
    cookieParser = require('cookie-parser'),
    bodyParser = require('body-parser');

    index = require('./routes/index'),
    app = express();

    DIST_DIR = path.join(__dirname, 'dist'),
    app.use(express.static(DIST_DIR));
    app.use('/', index);
    app.set('views', path.join(__dirname, 'views')),
    app.set('view engine', 'ejs');

    module.exports = app;
但我返回时出现了一个错误:每当我尝试加载页面时,都会出现空响应。我遗漏了一些东西,不知道在哪里。任何帮助都将不胜感激。

通常您有一个index.html,其中包含一个div,可以加载所有内容。如果您想从头开始构建所有内容,请参见以下内容:


或者,如果您想花更少的时间来处理工具,花更多的时间来处理react,那么请考虑使用上面更新的

,因为我的意思是在代码片段中编写bundle.js,而不是index.js。您是仅在bundle.js请求中,还是在整个页面中收到该错误?这些错误指向app.js中的一个问题,但是如果没有更多的信息,比如路由/路由器配置等,很难说。@robertklep我只得到bundle.js请求的错误。否则,整个页面将很好地加载。我在上面添加了我的app.js以获取更多上下文。当您直接请求捆绑文件时,是否也会出现错误?http://localhost:3000/bundle.js 假设服务器在端口3000上运行。如果没有,服务器是否将任何内容记录到控制台?@roberklep是的,在等待localhost一段时间后,我得到一个localhost没有发送任何数据ERR\u EMPTY\u响应,但控制台上没有任何内容。
var express = require('express');
var router = express.Router();

router.get('/', function(req, res, next) {
  res.render('home/index.ejs');
});

module.exports = router;
var express = require("express");
    path = require("path"),
    cookieParser = require('cookie-parser'),
    bodyParser = require('body-parser');

    index = require('./routes/index'),
    app = express();

    DIST_DIR = path.join(__dirname, 'dist'),
    app.use(express.static(DIST_DIR));
    app.use('/', index);
    app.set('views', path.join(__dirname, 'views')),
    app.set('view engine', 'ejs');

    module.exports = app;