詳細: このウォークスルーで使用されているアプリケーションの学習 - AWS OpsWorks

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

詳細: このウォークスルーで使用されているアプリケーションの学習

重要

AWS OpsWorks Stacks は新規顧客を受け付けなくなりました。既存のお客様は、2024 年 5 月 26 日までは OpsWorks コンソール、 API、 CLI、および CloudFormation リソースを通常どおり使用できますが、その時点でこれらのリソースは廃止されます。この移行に備えて、できるだけ早くスタックを AWS Systems Manager に移行することをおすすめします。詳細については、AWS OpsWorks Stacks サポート終了に関する FAQ および AWS Systems Manager アプリケーションマネージャへの AWS OpsWorks Stacks アプリケーションの移行 を参照してください。

このトピックでは、このウォークスルー用にインスタンスに AWS OpsWorks スタックによってデプロイされるアプリケーションについて説明します。

アプリケーションのソースコードを表示するには、opsworks-windows-demo-nodejs GitHub リポジトリの内容を、ローカルワークステーション上の空のディレクトリに展開します。クックブックをデプロイしたインスタンスにログインし、/srv/mylinuxdemoapp ディレクトリの内容を確認することもできます。

index.js ファイルには、アプリケーションで最も重要なコードが含まれています。

var express = require('express'); var app = express(); var path = require('path'); var os = require('os'); var bodyParser = require('body-parser'); var fs = require('fs'); var add_comment = function(comment) { var comments = get_comments(); comments.push({"date": new Date(), "text": comment}); fs.writeFileSync('./comments.json', JSON.stringify(comments)); }; var get_comments = function() { var comments; if (fs.existsSync('./comments.json')) { comments = fs.readFileSync('./comments.json'); comments = JSON.parse(comments); } else { comments = []; } return comments; }; app.use(function log (req, res, next) { console.log([req.method, req.url].join(' ')); next(); }); app.use(express.static('public')); app.use(bodyParser.urlencoded({ extended: false })) app.set('view engine', 'jade'); app.get('/', function(req, res) { var comments = get_comments(); res.render("index", { agent: req.headers['user-agent'], hostname: os.hostname(), nodeversion: process.version, time: new Date(), admin: (process.env.APP_ADMIN_EMAIL || "admin@unconfigured-value.com" ), comments: get_comments() }); }); app.post('/', function(req, res) { var comment = req.body.comment; if (comment) { add_comment(comment); console.log("Got comment: " + comment); } res.redirect("/#form-section"); }); var server = app.listen(process.env.PORT || 3000, function() { console.log('Listening on %s', process.env.PORT); });

ファイルで実行される操作は次のとおりです。

  • require は、このウェブアプリケーションを予期どおりに実行するために必要な一部の依存コードを含むモジュールをロードします。

  • add_comment 関数と get_comments 関数は、comments.json ファイルに対して情報の書き込みと読み取りを行います。

  • app.getapp.listenapp.postapp.set、および app.use の詳細については、「Express API Reference」を参照してください。

デプロイ用のアプリケーションの作成とパッケージ化の方法については、「Application Source」を参照してください。