角2无法调用节点服务器的Http调用

问题描述:

我没有调用到节点服务器的Http调用。角2无法调用节点服务器的Http调用

呼叫从以下组件启动:

@Component({ 
    selector: 'app-special-value', 
    templateUrl: './special-value.component.html', 
    styleUrls: ['./special-value.component.css'] 
}) 

export class SpecialValueComponent implements OnInit { 

    constructor(private dataService: DataService) { } 

    ngOnInit() { 
    this.getSpecialValueProducts(); 
    } 

    getSpecialValueProducts() { 
    this.dataService.getSpecialValueProducts(); 
    } 

} 

服务的getSpecialValueproducts()被调用它调用节点服务器:

@Injectable() 
export class DataService { 
private specialValueUrl = "/api/product/specialvalue"; 

    constructor(private http: Http) { } 

    getSpecialValueProducts() { 
    console.log('getSpecialValueProducts() called'); 

    return this.http.get(this.specialValueUrl) 
     .map((response: Response) => response.json()) 
     .catch(this.handleError); 
    } 
} 

到数据库的连接是成功的。但从数据服务到节点功能的后续调用从不会被调用。

const express = require('express'); 
const router = express.Router(); 
const mongoose = require('mongoose'); 

const Product = require('../models/product'); 

module.exports = router; 

const url = "mongodb://xxxxxx:[email protected]:111111/xxxxxx"; 

mongoose.Promise = global.Promise; 
mongoose.createConnection(url, function(err) { 
    if(err) { 
     console.log('Error!!!' + err); 
    } else { 
     console.log('Connected to Database!'); 
    } 
}); 

router.get('/product/specialvalue', function(req, res) { 
    console.log('Get specialvalue called '); 

Product.find({'specialValue': true}) 
.sort({'price': 1}) 
.exec(function(err, products) { 
    if(err) { 
     console.error('Error retrieving special value products!'); 
    } else { 
     console.log("products = " + JSON.stringify(products)); 
     res.json(products);   
    } 
}) 

}) 

节点服务器:

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

const api = require('./server/routes/api'); 
const port = 4200; 

const app = express(); 
app.use(express.static(path.join(__dirname, 'dist'))); 

app.use(bodyParser.urlencoded({extended: true})); 
app.use(bodyParser.json()); 

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

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

app.listen(port, function() { 
    console.log('@@@@ nglowes01 Server running on localhost: ' + port); 
}) 
+0

你可以显示你的应用程序安装路由器的位置? – Paul

+0

我不清楚你的问题。 – koque

+0

我已经包含节点服务器,如果这可以解决您的问题。 – koque

看起来你缺少subscribe。它不会执行http请求,直到您订阅。这样的事情:

@Component({ 
    selector: 'app-special-value', 
    templateUrl: './special-value.component.html', 
    styleUrls: ['./special-value.component.css'] 
}) 

export class SpecialValueComponent implements OnInit { 
    specialValue; 

    constructor(private dataService: DataService) { } 

    ngOnInit() { 
    this.getSpecialValueProducts(); 
    } 

    getSpecialValueProducts() { 
    this.dataService.getSpecialValueProducts().subscribe(value => this.specialValue = value); 
    } 

}