자세히 알아보기: 이 안내서에서 사용한 앱 살펴보기 - AWS OpsWorks

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

자세히 알아보기: 이 안내서에서 사용한 앱 살펴보기

중요

이 AWS OpsWorks Stacks 서비스는 2024년 5월 26일에 수명이 종료되었으며 신규 고객과 기존 고객 모두 사용할 수 없습니다. 고객은 가능한 한 빨리 워크로드를 다른 솔루션으로 마이그레이션할 것을 강력히 권장합니다. 마이그레이션에 대해 궁금한 점이 있으면 AWS re:Post 또는 Premium AWS Support를 통해 AWS Support 팀에 문의하세요.

이 주제에서는 이 안내를 위해 AWS OpsWorks Stacks가 인스턴스에 배포하는 앱을 설명합니다.

앱의 소스 코드를 보려면 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_commentget_comments 함수는 comments.json 파일에 정보를 작성하고 이 파일에서 정보를 읽어 옵니다.

  • app.get, app.listen, app.post, app.set, app.use에 대해서는 Express API Reference를 참조하세요.

배포를 위해 앱을 만들고 패키징하는 방법은 애플리케이션 소스 단원을 참조하세요.