2016-08-28 1 views
0

私はGitHubでホストされているnode.jsアプリケーションをAzureでホストしようとしています。私はまだ失敗した最小限の例にしました。GitHubでホストされているnode.jsアプリケーションをAzureで正しく実行するには

これは私の./server/index.jsファイルです:

var http = require('http'); 

var server = http.createServer(function (request, response){ 
    response.writeHead(200, {"Content-Type": "text/plain"}); 
    response.end("Hello world!\n"); 
}); 

server.listen(80); 
console.log("Server running at http://127.0.0.1:80/"); 

そして、これは私のpackage.jsonファイルです:

{ 
    "name": "testapp", 
    "version": "1.0.0", 
    "description": "", 
    "main": "./server/index.js", 
    "scripts": { 
     "start": "node ./server/index.js", 
     "test": "echo \"Error: no test specified\" && exit 1" 
    }, 
    "author": "", 
    "license": "ISC" 
} 

自分のアプリケーションのすべてのファイルです。 localhostでnpm startを起動するとうまく動作し、 "Hello world!"期待どおりの応答。

次に、GitHubのレポを作成し、このアプリケーションをGitHubにプッシュし、両方のファイルが存在することを確認しました。

次に、私はAzureにWeb appを作成し、このGitHubリポジトリからデプロイするように設定しました。ビルドはうまくいったようです。これはGenerating Deployment Scriptから出力されます:

Using cached version of deployment script (command: 'azure -y --no-dot- deployment -r "D:\home\site\repository" -o "D:\home\site\deployments\tools" --node --sitePath "D:\home\site\repository"'). 

そして、これはRunning Deployment Commandの出力です:

Command: "D:\home\site\deployments\tools\deploy.cmd" 
Handling node.js deployment. 
KuduSync.NET from: 'D:\home\site\repository' to: 'D:\home\site\wwwroot' 
Copying file: 'server\index.js' 
Using start-up script server/index.js from package.json. 
Generated web.config. 
The package.json file does not specify node.js engine version constraints. 
The node.js application will run with the default node.js version 4.2.3. 
Selected npm version 3.5.1 
npm WARN [email protected] No description 
npm WARN [email protected] No repository field. 
Finished successfully. 

そしてAzureがdelpoyment成功したと言います。

しかし、appname.azurewebsites.netに移動すると、Status Code500と応答し、応答データがまったくありません。

これが期待どおりに動作するには、ここでは欠けているパズルの部分は何ですか?アプリはAzureの上で実行されている場合、それはアズールによって割り当てられたポートを使用します

process.env.PORT = process.env.PORT || 80; 

server.listen(process.env.PORT); 

:あなたはこのような何かを行うことができますので

+0

「80」はアズールが世界に公開する予定のポートですか? – jonrsharpe

+0

これはdafault httpポートなので '80'を使用しましたが、特に何も設定していません。これはどこで設定できますか?私はAzureポータル内でこれの設定を見つけることができません。 –

答えて

1

Azureでは、process.env.PORTでアプリのポートを利用できるようになります。 localhost上で実行されている場合、代わりに80が使用されます。

+0

それはトリックでした!どうもありがとう。 –

+2

実際にはTCP PORTのようなPORTではなく、名前付きパイプです。 Azure App Serviceは** iisnode **を使用して、名前付きパイプ経由でノードプロセスと通信します。これを見てください:https://www.reddit.com/r/AZURE/comments/4iv0y4/deploy_socketio_app_on_azurewebservices/ – evilSnobu

関連する問題