【问题标题】:Deploy to heroku : Uncaught SyntaxError: Unexpected token <部署到heroku:未捕获的语法错误:意外的令牌<
【发布时间】:2018-11-29 13:00:03
【问题描述】:

我已将 mean stack api angular 6 部署到 heroku,当我在浏览器上打开应用程序时,它会显示空白页面,

在我的 server.js 中,这就是我所拥有的

const express = require('express');
const path = require('path');
const flash = require('connect-flash');
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const db = require('./app/config/database');

// Connect To Database
mongoose.connect(db.database);

// On Connection
mongoose.connection.on('connected', () => {
  console.log('Connected to database '+db.database);
});

// On Error
mongoose.connection.on('error', (err) => {
  console.log('Database error: '+err);
});

const app = express();

const movies = require('./app/routes/movies');

// Port Number
const port = process.env.PORT || 8000

// CORS Middleware
app.use(cors());

// Set Static Folder
app.use(express.static(path.join(__dirname, '/movies-client/dist')));

// Body Parser Middleware
app.use(bodyParser.json());

app.use('/movies', movies);

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname + '/movies-client/dist/movies-client/index.html'));
});
// Start Server
app.listen(port, () => {
  console.log('Server started on port '+port);
});

当我运行 heroku logs 时,这就是我所拥有的

2018-06-20T11:27:14.549707+00:00 heroku[router]: at=info method=GET path="/" host=damp-reef-60581.herokuapp.com request_id=f44c5f42-2bd0-42de-8a5d-18551c306fd0 fwd="212.91.22.46" dyno=web.1 connect=0ms service=50ms status=200 bytes=1191 protocol=https
2018-06-20T11:27:15.041658+00:00 heroku[router]: at=info method=GET path="/runtime.js" host=damp-reef-60581.herokuapp.com request_id=197ccaba-2fa8-42d6-8293-bc481fdf3fc3 fwd="212.91.22.46" dyno=web.1 connect=0ms service=16ms status=200 bytes=1191 protocol=https
2018-06-20T11:27:15.313302+00:00 heroku[router]: at=info method=GET path="/styles.js" host=damp-reef-60581.herokuapp.com request_id=46f87c24-9a16-4d45-82e0-295b184dceec fwd="212.91.22.46" dyno=web.1 connect=0ms service=3ms status=200 bytes=1191 protocol=https
2018-06-20T11:27:16.017486+00:00 heroku[router]: at=info method=GET path="/polyfills.js" host=damp-reef-60581.herokuapp.com request_id=7dd4bb06-ce65-472b-a8d3-106882666a11 fwd="212.91.22.46" dyno=web.1 connect=0ms service=9ms status=200 bytes=1191 protocol=https
2018-06-20T11:27:16.015745+00:00 heroku[router]: at=info method=GET path="/main.js" host=damp-reef-60581.herokuapp.com request_id=1ec5edd6-632d-4b37-9984-fd076fe9a083 fwd="212.91.22.46" dyno=web.1 connect=0ms service=6ms status=200 bytes=1191 protocol=https
2018-06-20T11:27:16.575143+00:00 heroku[router]: at=info method=GET path="/vendor.js" host=damp-reef-60581.herokuapp.com request_id=94178235-0013-49e6-aaa9-d34b84cc78ef fwd="212.91.22.46" dyno=web.1 connect=1ms service=3ms status=200 bytes=1191 protocol=https

当我检查浏览器时显示此错误:

Uncaught SyntaxError: Unexpected token <
polyfills.js:1 Uncaught SyntaxError: Unexpected token <
styles.js:1 Uncaught SyntaxError: Unexpected token <
vendor.js:1 Uncaught SyntaxError: Unexpected token <
main.js:1 Uncaught SyntaxError: Unexpected token <

我有 dist 文件的客户端应用程序的结构

为什么会出现这个错误?我该如何解决?任何帮助或建议都会有所帮助

【问题讨论】:

  • js文件中的HTML?
  • 从错误来看,您的服务器 URL 冲突似乎不正确。他们没有返回正确的请求。让我详细检查一下。
  • @Teemu 你是什么意思?
  • 我猜问题出在您的 html 文件配置中的通配符 * 上。
  • 因为它,您的应用在每个文件请求(包括 JS)上都返回 index.html 文件。

标签: node.js express


【解决方案1】:

刚开始调试您的应用,请尝试用斜杠替换通配符,因为通配符可以覆盖 /movies 路由

也许是你的道路

 app.use(express.static(path.join(__dirname, '/movies-client/dist')));

好像错了

 app.use(express.static(path.join(__dirname, '/movies-client/dist/movies-client')));

【讨论】:

  • 我还有一个问题,damp-reef-60581.herokuapp.com 来自服务器 api 的数据未加载,我的 locahost 以查看我使用的数据: locahost:8000/movies/movies ,我需要更改哪些内容才能加载这些数据文件?
  • 这可能是通配符路线,您可以尝试重用通配符而不是斜杠,并且您的 api url 将是 /movies 并且您可以按照本教程进行操作,我认为它很有用scotch.io/tutorials/mean-app-with-angular-2-and-the-angular-cli
  • 在您的 api 中为所有路由添加 /api 前缀,以免与您的应用路由器冲突
【解决方案2】:

我进行了以下更改以部署到 Heroku。

  1. index.html -
  2. server.js - app.use(express.static(__dirname + '/dist'));
  3. 根据建议更改 package.json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 2011-04-07
    • 2014-05-09
    • 2014-11-24
    • 2012-09-07
    • 2015-03-21
    • 1970-01-01
    相关资源
    最近更新 更多