Deploy to heroku : Uncaught SyntaxError: Unexpected token <

五迷三道 提交于 2020-01-04 02:53:10

问题


I have deployed mean stack api angular 6 to heroku , when I open the app on browser its displays brank page ,

In my server.js this is what i have

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);
});

when I run heroku logs , this is what I have

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

when I inspect the browser this error is shown :

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 <

structre of the client app where I have dist files

why am I getting this error? and how can I solve it? any help or suggestion will be helpfully


回答1:


Just for start debugging your app try to replace wildcard by a slash because wildcard can override /movies route

And may be your path

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

is wrong it seems to be

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


来源:https://stackoverflow.com/questions/50947450/deploy-to-heroku-uncaught-syntaxerror-unexpected-token

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!