【问题标题】:Deploying to heroku: Invalid end points部署到heroku:端点无效
【发布时间】:2018-11-29 08:21:15
【问题描述】:

我正在将我的应用程序部署到 heroku,我按照他们在其文档中指示的所有步骤进行部署,当我运行该应用程序时,我在浏览器中收到以下错误:

Invalid End point

这是我的服务器

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

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

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

// Index Route
app.get('/', (req, res) => {
  res.send('Invalid Endpoint');
});

// Start Server
app.listen(port, () => {
  console.log('Server started on port '+port);
});

这是我的 service.ts

    import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import { Jsonp } from '@angular/http';

@Injectable({
  providedIn: 'root'
})
export class MoviesService {
  queryUrl = '/search/';
  // private apiUrl = '/movies';
  constructor(private http: Http, private _jsonp: Jsonp) { }
  getMovies(id: string): Promise<any> {
    return this.http.get('movies/movies')
      .toPromise()
      .then(this.handleData)
      .catch(this.handleError);
  }
  getMovie(id: string): Promise<any> {
    return this.http.get('movies/movies' + id)
      .toPromise()
      .then(this.handleData)
      .catch(this.handleError);
  }
  addReview(author, description) {
    const uri = 'movies/comments';
    const obj = {
      author: author,
      description: description
    };
    this
      .http
      .post(uri, obj)
      .subscribe(res =>
          console.log('Done'));
  }
  getReview(id: string): Promise<any> {
    return this.http.get('movies/movies' + id)
      .toPromise()
      .then(this.handleData)
      .catch(this.handleError);
  }
  searchMovies(searchStr: string): Promise<any> {
    return this.http.get('movies/search' + this.queryUrl + searchStr)
      .toPromise()
      .then(this.handleData)
      .catch(this.handleError);
  }
  getInTheaters(): Promise<any> {
    return this.http.get('movies/theatre')
      .toPromise()
      .then(this.handleData)
      .catch(this.handleError);
  }
  createMovie(movie: string): Promise<any> {
    return this.http.post('movies/movies', movie)
      .toPromise()
      .then(this.handleData)
      .catch(this.handleError);
  }
  private handleData(res: any) {
    const body = res.json();
    console.log(body); // for development purposes only
    return body.result || body || {};
  }
  private handleError(error: any): Promise<any> {
    console.error('An error occurred', error); // for development purposes only
    return Promise.reject(error.message || error);
  }
}

这是我的私人 api 网址:

 private apiUrl = 'http://localhost:8000/movies';

示例:如果我运行 http://localhost:8000/movies/movies 我得到所有电影,仅供参考:

我的代码中的端点有什么问题?我需要改变什么?

【问题讨论】:

    标签: node.js angular express heroku


    【解决方案1】:

    您好,将此添加到您的服务器代码中:

     app.use(express.static(path.join(__dirname, '/movies-client/dist/movies-client')));
    
        app.use('/movies', movies);
        app.get('/', (req, res) => {
          res.send('invaild endpoint');
        });
    
        app.get('*', (req, res) => {
          res.sendFile(path.join(__dirname + '/movies-client/dist/movies-client/index.html'));
        });
    

    【讨论】:

      猜你喜欢
      • 2019-11-05
      • 1970-01-01
      • 2016-10-19
      • 2021-05-13
      • 2021-12-09
      • 1970-01-01
      • 2020-01-13
      • 2017-02-02
      相关资源
      最近更新 更多