我正在为我的电子商务商店使用MEAN堆栈,尝试在Heroku中打开我的应用程序时出现内部服务器错误。我已经更改了必须在其中指定目录的行,但是仍然出现错误。
这是我的app.js文件(到我的数据库服务器的链接在database.js文件中):
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const mongoose = require('mongoose');
const config = require('./config/database');
// Connect To Database
mongoose.connect(config.database);
// On Connection
mongoose.connection.on('connected', () => {
console.log('Connected to database '+config.database);
});
// On Error
mongoose.connection.on('error', (err) => {
console.log('Database error: '+err);
});
const app = express();
const users = require('./routes/users');
const products = require('./routes/products');
// Port Number
// const port = 3000;
const port = process.env.PORT || 8080;
// CORS Middleware
app.use(cors());
//Enable CORS for all HTTP methods
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Set Static Folder
app.use(express.static(path.join(__dirname, 'src')));
// Body Parser Middleware
app.use(bodyParser.json());
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
app.use('/users', users);
// app.use('/products', products);
// Index Route
app.get('angular-src/src/index.html', (req, res) => {
res.send('Invalid Endpoint');
});
app.get('*',(req,res)=>{
res.sendFile(path.join('angular-src/src/index.html'));
})
// Start Server
app.listen(port, () => {
console.log('Server started on port '+port);
});
这是我的文件夹结构。我知道这有点混乱。
需要更多详细信息,请让我知道。