2016-10-12 4 views
0

私はHerokuに自分のapiアプリケーションをデプロイし、通常dyno上で実行しています。Herokuは常にデータをjsonで返す代わりにhtmlとして応答を送信します

私の問題は、私はPOSTMANを使用して要求または私のフロントエンドアプリケーションを実行しようとするとき、Herokuのサーバは常にそのHTMLコンテンツを返します:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> 
<html> 
<head> 
<meta http-equiv='content-type' content='text/html; charset=UTF-8'> 
<meta name="VERSION" content=""> 
<meta name="DATA" content="fwdnode3.registrar-servers.com (208.64.122.246)"> 
<link href=" " rel="shortcut icon" type="image/x-icon"> 
<title></title> 
</head> 
<frameset rows='100%, *' frameborder=no framespacing=0 border=0> 
<frame src="https://epiphany-api.herokuapp.com/api/v1/products" name=mainwindow frameborder=no framespacing=0 marginheight=0 marginwidth=0></frame> 
</frameset> 
<noframes><h2>Your browser does not support frames. We recommend upgrading your browser.</h2><br><br> 
<center>Click <a href="https://epiphany-api.herokuapp.com/api/v1/products" >here</a> to enter the site.</center> 
</noframes> 
</html> 
</pre> 

上記応答HTTPコード200(OK)と応答でありますヘッダー:

Server: nginx 
Date: Wed, 12 Oct 2016 12:56:23 GMT 
Content-Type: text/html 
Transfer-Encoding: chunked 
Connection: keep-alive 

ただし、ブラウザを使用してリクエストを行うと、Herokuサーバーはデータを正しく返します。

Nginxサーバーから400(Bad Resquest)コードも受け取ることがあります。

私のapiはノードアプリケーションなので、なぜNginxサーバーがノードアプリケーションを代行して応答を送信しているのかわかりません。

編集 - 追加説明

は、これは私のAPIのNodeJSコードです:

'use strict'; 

const express = require('express'), 
    app = express(), 
    consign = require('consign'), 
    bodyParser = require('body-parser'), 
    cors = require('cors'), 
    compression = require('compression'), 
    port = process.env.PORT || 9000; 

app.use(cors()); 
app.use(compression()); 
app.use(bodyParser.json({ limit: '50mb' })); 

consign({ cwd: 'project' }) 
    .include('common/config') 
    .then('common/helpers') 
    // middlewares should be loaded individually 
    .then('common/middleware/authenticationV1.js') 
    .then('modules/products/validationsV1.js') 
    .then('modules/products/repositoryV1.js') 
    .then('modules/products/modelV1.js') 
    .then('modules/products/controllerV1.js') 
    .then('modules/products/routesV1.js') 
    .then('modules/categories/repositoryV1.js') 
    .then('modules/categories/modelV1.js') 
    .then('modules/categories/controllerV1.js') 
    .then('modules/categories/routesV1.js') 
    .then('modules/checkout') 
    .then('modules/colors/repositoryV1.js') 
    .then('modules/colors/modelV1.js') 
    .then('modules/colors/controllerV1.js') 
    .then('modules/colors/routesV1.js') 
    .then('modules/sizes/repositoryV1.js') 
    .then('modules/sizes/modelV1.js') 
    .then('modules/sizes/controllerV1.js') 
    .then('modules/sizes/routesV1.js') 
    .then('modules/users/modelV1.js') 
    .then('modules/users/controllerV1.js') 
    .then('modules/users/routesV1.js') 
    .then('modules/adminUsers/repositoryV1.js') 
    .then('modules/adminUsers/modelV1.js') 
    .then('modules/adminUsers/controllerV1.js') 
    .then('modules/adminUsers/routesV1.js') 
    //the error handler has to be last middleware loaded, pay attention :) 
    .then('common/middleware/error-handlerV1.js') 
    .into(app); 

app.listen(port,() => { 
    console.log('running server port %s', port); 
}); 

ちょうどそのエンドポイントにGETリクエストを送信し、テストするために: http://api.epiphany.store/api/v1/products

編集 - 説明を追加

modules/colors/routesV1.js

'use strict'; 

module.exports = app => { 
    const colors = app.modules.colors.controllerV1; 
    const auth = app.common.middleware.authenticationV1; 

    app.get('/api/v1/colors', colors.getAll); 
    app.get('/api/v1/colors/:id', colors.get); 
    app.post('/api/v1/colors', auth.admin, colors.post); 
    app.put('/api/v1/colors', auth.admin, colors.put); 
    app.delete('/api/v1/colors/:id', auth.admin, colors.delete); 
} 

modules/colors/controllerV1.js

'use strict'; 

module.exports = app => { 
    const Colors = app.modules.colors.modelV1; 

    this.getAll = (req, res, next) => { 
     Colors.find() 
      .then(result => { 
       res.json(result); 
      }) 
      .catch(err => next(err)); 
    }; 

    this.get = (req, res, next) => { 
     Colors.findById(req.params.id) 
      .then(result => { 
       if (!result) { 
        res.status(404).end();  
       } 

       res.json(result); 
      }) 
      .catch(err => next(err)); 
    }; 

    this.post = (req, res, next) => { 
     Colors.create(req.body) 
      .then(result => { 
       res.status(204).json(result); 
      }) 
      .catch(err => next(err)); 
    }; 

    this.put = (req, res, next) => { 
     Colors.update(req.body) 
      .then(result => { 
       res.status(200).end(); 
      }) 
      .catch(err => next(err)); 
    }; 

    this.delete = (req, res, next) => { 
     Colors.findById(req.params.id) 
      .then(result => { 
       if (!result) { 
        res.status(404).end(); 
       } 

       result.remove(err => { 
        if (err) { 
         next(err); 
        } 

        res.status(200).end(); 
       }); 
      }) 
      .catch(error => next(error)); 
    }; 

    return this; 
} 

modules/colors/modelV1.js

'use strict'; 

module.exports = app => { 
    const mongoose = app.common.config.mongooseV1; 
    const repository = app.modules.colors.repositoryV1; 
    const Product = app.modules.products.modelV1; 
    const validationError = app.common.helpers.errors.validationV1; 

    repository.pre('remove', function (next) { 
     Product.count({ 'stock.color': this._id }) 
      .then(quantity => { 
       if (!quantity) { 
        next(); 
       } 

       let error = validationError(this, { 
        action: 'remove', 
        errors: { 
         impossibleToRemove: { 
          message: 'This registry is being used by others registries' 
         } 
        } 
       }); 

       next(error); 
      }) 
      .catch(error => next(error)); 
    }); 

    return mongoose.model('colors', repository); 
} 

modules/colors/repositoryV1.js

'use strict'; 

module.exports = app => { 
    const mongoose = app.common.config.mongooseV1; 

    const schema = mongoose.Schema({ 
     description: { 
      type: String, 
      required: [true, 'Required field'], 
      unique: [true, 'Unique field'] 
     }, 
     color: { 
      type: String, 
      required: [true, 'Required field'] 
     } 
    }); 

    return schema; 
} 

ワークフローはroute -> controller -> model -> repositoryです。構造ワークフロー

改善が

はすべての助けをありがとう:)大歓迎です!

+0

データを送信するコードを貼り付けます。 NodeJS側にあるコード。 –

+0

こんにちは@David、私は私の質問を変更しました。 – henriquecustodia

+0

私たちはほとんどそこにいます:)リクエストを処理するJSファイルの1つのコンテンツが必要です。 –

答えて

0

私は、以下の方法で、私の問題を修正しました:

httpProxy.createProxyServer({ 
    target: 'http://my.app.com', 
    toProxy: true, 
    changeOrigin: true, 
    xfwd: true 
}); 

Herokuのは、私のアプリのためにリダイレクトするプロキシを持って、私はtoProxychangeOriginプロパティを追加する私の問題を修正しました。

ああ、ええ、私は答えは、誰かを助けることを願って、そのノードモジュールにhttps://github.com/nodejitsu/node-http-proxy

を使用しています!

関連する問題