刷新页面时,我的MEAN堆栈应用正在浏览器中返回原始json而不是HTML。如果我通过路由按钮访问页面或来回浏览,那就很好。但是,如果我手动输入网址或刷新我所在的页面,浏览器会显示我的原始数据。
我在控制台或服务器上没有收到错误。
我的服务中的示例方法:
getSingleCompany(category, company) {
return this.http.get(`${this.BASE_URL}/${category}/${company}`)
.map((res) => res.json())
.catch((err) => {
return Observable.throw(err);
});
}
路线:
export const routes: Routes = [
{ path: '', component: CategoriesComponent },
{ path: 'category/men-apparel', component: MenApparelComponent },
{ path: 'category/womens-apparel', component: WomenApparelComponent },
{ path: 'profile/:id', component: ProfileComponent, canActivate: [AuthService] },
{ path: ':category/:company', component: CompanyProfileComponent },
];
服务器:
//Get our API routes
const api = require("./server/routes/api");
const auth = require("./server/routes/auth");
每条路由,但我的身份验证路由(用于登录和注册)位于我的api中
app.use('/', api);
app.use('/', auth);
所有内容都被编译到一个保存着index.html的dist文件夹中
//Catch all other routes and return the index file.
//Catch-all route MUST come after all other API routes have been defined.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
路线示例:
//Retrieve all the reviews for the company and populate
router.get('/:category/:company', (req, res, next) => {
Company.findOne({ "companyName": req.params.company}, (err, company) => {
// console.log("printing all reviews", company.reviews);
if(!company) {
res.status(400).json({ message: "Can't find the company you're looking for at the moment." });
} else {
Review.find({'_id': { $in: company.reviews }})
.populate("createdBy")
.exec((err, review) => {
if (err) {
next(err);
return;
} else {
res.json({
message: "Retrieving your company",
company: company,
reviews: review
});
console.log("returning review", review);
}
});
}
});
});
我觉得我缺少明显的东西。一直在尝试不同的事情,但无法看到我在做什么错。想知道这里是否有熟练的Angular / Node专家会知道我在做什么错。
谢谢您的光临。