【问题标题】:Deploying MEAN stack, Mongo no output result部署MEAN堆栈,Mongo没有输出结果
【发布时间】:2020-12-29 11:22:37
【问题描述】:

我有一个 MEAN 项目。我正在将此部署到 Heroku,但无法获取 Mongo 数据。我在后端文件夹中构建 Angular 应用程序。

文件路径:

--ProjectName
 |-frontend (angular app)
 |-backend (node.js server)
  |-public (Folder where angular project gets built)

将其用于 Heroku 部署:https://devcenter.heroku.com/articles/getting-started-with-nodejs#deploy-the-app

我的节点服务器为:

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();

const mongoose = require('./database/mongoose');

const Confession = require('./database/confession');
const Art = require('./database/art');

const PORT = process.env.PORT || 3100;

app.use(express.json());

app.use(express.static(path.join(__dirname, 'public')));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}))
app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE, PUT, OPTIONS');
  res.setHeader('Access-Control-Allow-Credentials', 'true');
  next();
});

app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

.....

app.listen(PORT, () => console.log('Server running on port 3100'));

向服务器添加了 mongoDB Atlas,添加了用户名、密码和 dbname:

const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

mongoose.connect('mongodb+srv://<username>:<password>@cluster0.5zol8.mongodb.net/<dbname>?retryWrites=true&w=majority', { useNewUrlParser: true, useUnifiedTopology: true})
    .then(() => console.log('Database connected'))
    .catch((error) => console.log(error));

module.exports = mongoose;

这会部署 Angular 前端应用,但无法获取 MongoDB Atlas 数据。我得到错误:

HttpErrorResponse {
  headers: HttpHeaders, 
  status: 404, 
  statusText: "Not Found", 
  url: "http://localhost:3100/confessions", 
  ok: false, 
  …
}

这是我的网络服务:

@Injectable({
  providedIn: 'root'
})
export class WebService {
  readonly ROOT_URL;
  constructor(private http: HttpClient) {}

  get(uri: String) {
    return this.http.get(`${uri}`);
  }
}

我该如何解决这个问题?出了什么问题?

【问题讨论】:

  • 将代码部署到服务器后,localhost url 将不再工作。尝试用必要的域 URL 替换它们。在您的情况下,我希望 'localhost:4200' 应该替换为您的 heroku 应用程序网址。如需深入了解,请在上方提供您的角度代码。
  • 抱歉应该是 localhost:3100,因为我正在使用 nodemon 运行应用程序。还是不行。
  • 您确定此应用在本地环境中正常运行吗?
  • 试试我下面提到的答案,让我知道结果。

标签: node.js angular mongodb heroku mean-stack


【解决方案1】:

bodyParserpackage 导入到您的快递应用并设置CORS 政策如下。 这可能会有所帮助。

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}))
app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE, PUT, OPTIONS');
  res.setHeader('Access-Control-Allow-Credentials', 'true');
  next();
});

【讨论】:

  • 没有没有用。您还需要信息吗?我尽我所能。
  • 更新问题
  • app.get("/sendfile", function(req,res){ app.set('view engine', 'html'); res.status(200).sendFile(__dirname + 'public/index.html'); }); 这对我有用
【解决方案2】:
app.get("/sendfile", function(req,res){          
   app.set('view engine', 'html');          
   res.status(200).sendFile(__dirname + 'public/index.html');  
});

这对我有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-24
    • 2014-11-10
    • 2018-07-06
    • 2018-12-21
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 2017-06-30
    相关资源
    最近更新 更多