您知道我是否可以使用MeteorJS(或直接使用NodeJS)获取HTTP状态代码?我已阅读HTTP Api文档,但没有结果:(
为了设置我的路线,我使用Iron Router。
我喜欢获取我的页面状态代码以添加prerender balise:
<meta name="prerender-status-code" content="404">
我在最新版本上使用MeteorJS:)
谢谢社区!
答案 0 :(得分:0)
从文档中,传递给HTTP.call的回调将使用2个参数调用:error,result。 Result是一个具有statusCode属性的对象,如果请求成功,则为一个数字,出错时为null。
在异步模式下运行时,回调会收到两个参数,错误和结果。如果请求以任何方式失败,则错误参数将包含错误,包括网络错误,超时或400或500范围内的HTTP状态代码。对于4xx / 5xx HTTP状态代码,错误时的响应属性与结果对象的内容匹配。在同步模式下运行时,将从函数返回结果,或者抛出错误。
结果对象的内容:
statusCode Number
数字HTTP结果状态代码,或者出错时为null。
来自文档的示例代码:
HTTP.call('POST', 'http://api.twitter.com/xyz', {
data: { some: 'json', stuff: 1 }
}, () => (error, result) {
if (!error) {
console.log(result.statusCode);
}
});
答案 1 :(得分:0)
您可以尝试使用:Iron.Location.get().path;
获取最新链接。并使用Andre的解决方案来查找状态代码。
答案 2 :(得分:0)
要告诉prerender未找到您的页面或数据,您必须在页面标题中添加以下代码。
<meta name="prerender-status-code" content="404">
为此,我使用https://github.com/kadirahq/meteor-dochead。
Router.route('my-route', {
path: ['/my-route/:param1'],
waitOn: function () {
return [
Meteor.subscribe('myCollection', this.params.param1)
];
},
data: function () {
var data = myCollection.findOne({});
if (this.ready() && !data) {
var metaInfo = {name: "prerender-status-code", content: "404"};
DocHead.addMeta(metaInfo);
}
return {
routerData: {
data: data
}
}
}
});
您也可以在默认的PageNotFound模板上进行设置:
Template.PageNotFound.rendered = function() {
var metaInfo = {name: "prerender-status-code", content: "404"};
DocHead.addMeta(metaInfo);
});