0
我沒有調用到節點服務器的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);
})
你可以顯示你的應用程序安裝路由器的位置? – Paul
我不清楚你的問題。 – koque
我已經包含節點服務器,如果這可以解決您的問題。 – koque