Node.js Ionic/Angular未正确连接到节点(路线在邮递员中工作正常

Node.js Ionic/Angular未正确连接到节点(路线在邮递员中工作正常,node.js,angular,express,Node.js,Angular,Express,我可以使用POSTMAN运行GET&POST请求,并且它可以工作。我无法找出我的应用程序中哪里出了问题。我对同一地址的GET请求工作正常 例如,在我的服务中: createOne(route, document) { console.log(`create new ${route}`, document); console.log(`http://localhost:8080/api/${route}`); return this.http.post(`http://localhos

我可以使用POSTMAN运行GET&POST请求,并且它可以工作。我无法找出我的应用程序中哪里出了问题。我对同一地址的GET请求工作正常

例如,在我的服务中:

createOne(route, document) {
  console.log(`create new ${route}`, document);
  console.log(`http://localhost:8080/api/${route}`);
  return this.http.post(`http://localhost:8080/api/${route}`, document);
}
这将作为以下内容登录到控制台:

不幸的是,它没有在nodeJS服务器上记录任何东西,所以我确信我在server.js文件中的某个地方犯了一个错误——但我不知道在哪里

通过邮递员将请求发送至
http://localhost:8080/api/appts
对于原始JSON正文:

{
    "title": "test from postman",
    "description": "test description",
    "startTime": "2020-08-04T17:40:45.521-04:00",
    "endTime": "2020-08-04T20:10-04:00",
    "allDay": false
}

My server.js文件:

//1. require express and call it as a function
require('./api/data/db.js');
const cron = require('node-cron');
var express = require('express');
console.log(process.env.PORT);
var app = express();
var cors = require('cors');
var path = require('path');
var bodyParser = require('body-parser');
var routes = require('./api/routes');
var json2xls = require('json2xls');

//PORT SETUP
if (process.env.PORT) {
  app.use(cors());
  app.set('port', process.env.PORT || 8080);
} else {
  app.set('port', 8080);
}
// MIDDLEWARE
app.use(function (req, res, next) {
  console.log(req.method, req.url);
  next();
});
app.use(json2xls.middleware);

// ROUTING
app.use(express.static(path.join(__dirname, 'app/www'))); //setup primary route request folder
app.use('/node_modules', express.static(path.join(__dirname, 'node_modules')));
app.use(bodyParser.urlencoded({ extended: true })); //only get strings and arrays from form when false
app.use(bodyParser.json()); // tell backend to understand json data
app.use('/api', routes);
// LOAD THE SINGLE PAGE FOR IONIC
app.get('*', function (req, res) {
  res.sendFile('/app/www/index.html', { root: __dirname });
});

// START SERVER LISTENING
var server = app.listen(app.get('port'), function () {
  var port = server.address().port; // return the port for the server listening
  console.log('RayWood API running on port ' + port); //confirm app running
});
我的文件树设置如下:


看到nodeJS服务器没有记录URL或请求,我想问题出在我的server.js文件中-但我仍然不知道我做错了什么。请帮助!

您没有订阅Observable。如果您不熟悉Observable,可以将其转换为Promise,请使用下面的示例实现

createOne(route, document) {
      console.log(`create new ${route}`, document);
      console.log(`http://localhost:8080/api/${route}`);
    return new Promise((resolve, reject) => {
      
    
    this.http.post(`http://localhost:8080/api/${route}`,document).subscribe((resp)=>{
        console.log(resp);//This is the response from Node API
        resolve(resp);
    },(err)=>{
         reject(err)
    });
    
    });
    }

啊!在我写了一篇文章之后,我倾向于找出问题所在。它是连接的,但我调用了函数,它将其视为可观察的。一旦我订阅,它就工作了!还必须使用。cors()if语句的一部分。谢谢!我试图从组件订阅它,而不是在服务中。我一直试图抓住可观察的,但就是不能完全得到它。