【问题标题】:Node server returning only index.html don't load bundle.js or other js files仅返回 index.html 的节点服务器不加载 bundle.js 或其他 js 文件
【发布时间】:2017-02-23 23:06:55
【问题描述】:

你好 stackoverflow 成员,

这是我的第一个问题,所以请温柔。

目前我有一个运行 node.js 的 Amazon-web-service(AWS) 服务器。 我已经从 AWS 的 node.js 示例项目中复制了 node.js 文件。

////node.js
var port = process.env.PORT || 3666,
    http = require('http'),
    fs = require('fs'),
    html = fs.readFileSync('index.html');

var log = function(entry) {
    fs.appendFileSync('/tmp/sample-app.log', new Date().toISOString() + ' - ' + entry + '\n');
};
var server = http.createServer(function (req, res) {
    if (req.method === 'POST') {
        var body = '';

        req.on('data', function(chunk) {
            body += chunk;
        });

        req.on('end', function() {
            if (req.url === '/') {
                log('Received message: ' + body);
            } else if (req.url = '/scheduled') {
                log('Received task ' + req.headers['x-aws-sqsd-taskname'] + ' scheduled at ' + req.headers['x-aws-sqsd-scheduled-at']);
            }

            res.writeHead(200, 'OK', {'Content-Type': 'text/plain'});
            res.end();
        });
    } else {
        res.writeHead(200);
        res.write(html);
        res.end();
    }
});

// Listen on port 3000, IP defaults to 127.0.0.1
server.listen(port);

// Put a friendly message on the terminal
console.log('Server running at http://127.0.0.1:' + port + '/');

我如何在标题中写我只得到 index.html。 这就是它在控制台中的样子

bundle.js:1 Uncaught SyntaxError: Unexpected token

如果我在网络选项卡中打开 bundle.js(来自 chrome),则只有 index.html 中的代码

////index.html 
<!doctype html>
<html class="no-js" lang="ger" dir="ltr">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <script type="text/javascript" src="bundle.js" charset="utf-8"></script>
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.6.7.js" type="application/javascript"></script>
</head>
<body>
<h1>test</h1>

<div ng-view></div>
<script>
            $(document).foundation();
    </script>
</body>

</html>

Node JS and Webpack Unexpected token < 这里有其他人同样的问题(可能),但我了解他们只推荐了其他服务器应用程序(Express)到什么程度? 但是因为我使用 AWS 我只能使用 node.js

和bundle.js无关这个问题有任何JavaScript文件。

我没有使用 node.js 的经验,我只想加载 Javascript 文件。

解决方法:我可以正确使用和加载 http 调用。但这非常耗时。我需要在我的 AWS s3 存储桶上加载所有 Js 文件(甚至是我的模块控制器)。

这是一个单页应用程序,包含许多自定义 Javascript 文件和模块。 Here a screenshot from my directory

期待你的回答!

【问题讨论】:

  • 发布您的 HTML 文件。
  • but how far I've understand they've only recommended an other server application(Express)? But cause I use AWS I can only use node.js 仅供参考,Express 是一个节点模块,您可以在任何节点应用程序中使用 express,无论它在何处运行,无论是 AWS、Heroku 还是您的本地机器。
  • 这是一个 webpack 错误!!,您的 Node 部分和 HTML 无关紧要。问题在于构建捆绑包。
  • @todarist 你可以访问任何其他 js 文件吗?还是css文件?如果是,则检查 bundle.js 文件路径是否正确。当路径不可访问时也会出现此错误。
  • 我认为这个问题在这里是相关的stackoverflow.com/questions/33260093/…

标签: javascript angularjs node.js amazon-web-services webpack


【解决方案1】:

您的 Node 服务器被配置为只为您的 index.html 文件提供服务。

您的http.createServer 方法实现可以分解为:

When a request comes in
  If it is a POST request
    Process the request
  else
    Serve index.html  

因此,当对bundle.js 发出请求时,您正在为index.html 提供服务。由于bundle.js 应为javascript,因此在读取&lt; 令牌时会引发错误,因为它不是有效的javascript 令牌。

解决这个问题应该分解成几个问题。您应该从在本地设置工作服务器开始 - 使用 Express 之类的东西可以帮助简化为不同请求提供不同文件的过程。

【讨论】:

  • 没错。使用 Express 提供静态内容。或者您可以自己编写逻辑,例如:如果请求 url 以 .js 或 .css 结尾,则从您的静态文件夹(保存 bundle.js 的位置)中查找并提供文件,如果它是其他任何东西,则提供索引 html
猜你喜欢
  • 2017-01-06
  • 2019-06-26
  • 1970-01-01
  • 1970-01-01
  • 2015-10-05
  • 2014-03-11
  • 1970-01-01
  • 2017-12-29
  • 2019-01-23
相关资源
最近更新 更多