繁体   English   中英

仅返回index.html的节点服务器不加载bundle.js或其他js文件

[英]Node server returning only index.html don't load bundle.js or other js files

您好stackoverflow成员,

这是我的第一个问题,请保持温柔。

目前,我有一台正在运行node.js的Amazon Web服务(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未捕获的SyntaxError:意外的令牌<

如果我在Network标签(从Chrome)中打开bundle.js,则只有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和Webpack意外令牌<这里有另一个人遇到同样的问题(可能是),但是据我了解他们仅推荐了另一个服务器应用程序(Express)? 但是因为我使用AWS我只能使用node.js

与bundle.js无关,此问题包含任何JavaScript文件。

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

解决方法:我可以正确使用和加载http调用。 但这非常耗时。 我需要将所有Js文件(甚至是我的Mod Controller)加载到我的AWS s3存储桶中。

它是一个单页应用程序,具有许多自定义Javascript文件和模块。 这是我目录中的屏幕截图

期待您的回答!

您的节点服务器配置为仅提供您的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,因此读取<令牌时会引发错误,因为它不是有效的javascript令牌。

解决这个问题应分解为几个问题。 您应该首先在本地设置可以正常工作的服务器-使用Express等工具可以帮助简化为不同请求提供不同文件的过程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM