我设法在heroku上部署了应用程序,这是 链接:https://damp-reef-60581.herokuapp.com
当您导航到此链接时:https://damp-reef-60581.herokuapp.com/ 通常显示整个页面,
但是当您导航到https://damp-reef-60581.herokuapp.com/movies时
您会看到错误:Cannot GET /movies
在本地主机中:我导航至以下路由:
http://localhost:8000/movies/movies
http://localhost:8000/movies/review
http://localhost:8000/movies/theatre
我需要在我的server.js中进行什么更改才能使其在heroku中正常运行?
here is my server:
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/movies-client')));
// 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);
});
答案 0 :(得分:0)
我想我明白您的问题所在。在您的app.js
中,您拥有以下内容:
app.use('/movies', movies);
然后在您的./app/routes/movies
中打赌您有类似的东西
router.get('/movies', function(req, res, next){ ....
或类似的东西。问题是两个/movies
都加了。您要告诉服务器“在路径/movies
上使用路由器movies
。但是在movies
路由器内部,您还有另一个/movies
。因此,如果要使用这样的路由:localhost:8000/movies
应该在./app/routes/movies
内定义路线,如下所示:
router.get('/', function(req, res, next){ ....
因为您已经告诉服务器您在/movies
内部的app.js
路径中