使用MeteorJS获取HTTP状态代码

时间:2022-06-22 20:17:50

Do you know if i can get the HTTP status code with MeteorJS (or directly with NodeJS) ? I've read on HTTP Api documentation, but no result :(

你知道我是否可以通过MeteorJS(或直接使用NodeJS)获取HTTP状态代码?我已阅读HTTP Api文档,但没有结果:(

For setting my routes, i use Iron Router.

为了设置我的路线,我使用铁路由器。

I woud like get my page status code for add prerender balise :

我想得到我的页面状态代码来添加prerender balise:

<meta name="prerender-status-code" content="404">

I use MeteorJS on latest version :)

我在最新版本上使用MeteorJS :)

Thank you community !

谢谢社区!

3 个解决方案

#1


0  

From the docs, the callback passed to HTTP.call will be called with 2 parameters: error, result. Result is an object with the statusCode property, which is a number if the request succeeded, null on error.

从文档中,传递给HTTP.call的回调将使用2个参数调用:error,result。 Result是一个具有statusCode属性的对象,如果请求成功,则为一个数字,出错时为null。

When run in asynchronous mode, the callback receives two arguments, error and result. The error argument will contain an Error if the request fails in any way, including a network error, time-out, or an HTTP status code in the 400 or 500 range. In case of a 4xx/5xx HTTP status code, the response property on error matches the contents of the result object. When run in synchronous mode, either result is returned from the function, or error is thrown.

在异步模式下运行时,回调会收到两个参数,错误和结果。如果请求以任何方式失败,则错误参数将包含错误,包括网络错误,超时或400或500范围内的HTTP状态代码。如果是4xx / 5xx HTTP状态代码,则错误时的响应属性与结果对象的内容匹配。在同步模式下运行时,将从函数返回结果,或者抛出错误。

Contents of the result object:

结果对象的内容:

statusCode Number

Numeric HTTP result status code, or null on error.

数字HTTP结果状态代码,或者出错时为null。

Example code from the docs:

来自文档的示例代码:

HTTP.call('POST', 'http://api.twitter.com/xyz', {
    data: { some: 'json', stuff: 1 }
}, () => (error, result) {
    if (!error) {
        console.log(result.statusCode);
    }
});

#2


0  

You can try using:Iron.Location.get().path; To get current link. And use the solution by Andre to find the status code.

您可以尝试使用:Iron.Location.get()。path;获取当前链接。并使用Andre的解决方案来查找状态代码。

#3


0  

To be able to tell prerender that your page or data was not found you have to add the following code to your page header.

为了能够告诉prerender您的页面或数据未找到,您必须将以下代码添加到页面标题中。

<meta name="prerender-status-code" content="404">

I use https://github.com/kadirahq/meteor-dochead for that.

我使用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
            }
        }
    }
});

You can also set it on your default PageNotFound template:

您也可以在默认的PageNotFound模板上进行设置:

Template.PageNotFound.rendered = function() {
    var metaInfo = {name: "prerender-status-code", content: "404"};
    DocHead.addMeta(metaInfo);
});

#1


0  

From the docs, the callback passed to HTTP.call will be called with 2 parameters: error, result. Result is an object with the statusCode property, which is a number if the request succeeded, null on error.

从文档中,传递给HTTP.call的回调将使用2个参数调用:error,result。 Result是一个具有statusCode属性的对象,如果请求成功,则为一个数字,出错时为null。

When run in asynchronous mode, the callback receives two arguments, error and result. The error argument will contain an Error if the request fails in any way, including a network error, time-out, or an HTTP status code in the 400 or 500 range. In case of a 4xx/5xx HTTP status code, the response property on error matches the contents of the result object. When run in synchronous mode, either result is returned from the function, or error is thrown.

在异步模式下运行时,回调会收到两个参数,错误和结果。如果请求以任何方式失败,则错误参数将包含错误,包括网络错误,超时或400或500范围内的HTTP状态代码。如果是4xx / 5xx HTTP状态代码,则错误时的响应属性与结果对象的内容匹配。在同步模式下运行时,将从函数返回结果,或者抛出错误。

Contents of the result object:

结果对象的内容:

statusCode Number

Numeric HTTP result status code, or null on error.

数字HTTP结果状态代码,或者出错时为null。

Example code from the docs:

来自文档的示例代码:

HTTP.call('POST', 'http://api.twitter.com/xyz', {
    data: { some: 'json', stuff: 1 }
}, () => (error, result) {
    if (!error) {
        console.log(result.statusCode);
    }
});

#2


0  

You can try using:Iron.Location.get().path; To get current link. And use the solution by Andre to find the status code.

您可以尝试使用:Iron.Location.get()。path;获取当前链接。并使用Andre的解决方案来查找状态代码。

#3


0  

To be able to tell prerender that your page or data was not found you have to add the following code to your page header.

为了能够告诉prerender您的页面或数据未找到,您必须将以下代码添加到页面标题中。

<meta name="prerender-status-code" content="404">

I use https://github.com/kadirahq/meteor-dochead for that.

我使用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
            }
        }
    }
});

You can also set it on your default PageNotFound template:

您也可以在默认的PageNotFound模板上进行设置:

Template.PageNotFound.rendered = function() {
    var metaInfo = {name: "prerender-status-code", content: "404"};
    DocHead.addMeta(metaInfo);
});