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