この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:4
ページ更新者:guest
更新日時:2026-06-11 13:35:53

タイトル: 値の表示と処理の記述
SEOタイトル: EJSの値の表示と処理の記述

この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。

<h2>簡単なサーバー</h2>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<thead>
		<tr>
			<th scope="col" style="background-color:#ffffff"><span style="font-size:16px">server.js</span></th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff">var http = require('http');<br />
			var ejs = </span><span style="color:#f1c40f">require('ejs');</span><br />
			<span style="color:#ffffff">var fs = </span><span style="color:#f1c40f">require('fs');</span></p>

			<p><span style="color:#ffffff">var ejs_template = </span><span style="color:#f1c40f">fs.readFileSync('test.ejs', 'utf-8')</span><br />
			<span style="color:#ffffff">var server = http.createServer(function (req, res) {<br />
			&nbsp; &nbsp; var params = </span><span style="color:#f1c40f">ejs.render</span><span style="color:#ffffff">(<br />
			&nbsp; &nbsp; &nbsp; &nbsp; ejs_template, {<br />
			&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val1: '<strong>val1</strong>',<br />
			&nbsp; &nbsp; &nbsp; &nbsp; }<br />
			&nbsp; &nbsp; )<br />
			&nbsp; &nbsp; res.writeHead(200, {'Content-type': '</span><span style="color:#f1c40f">text/html</span><span style="color:#ffffff">'});<br />
			&nbsp; &nbsp; res.write(params);<br />
			&nbsp; &nbsp; res.end();<br />
			});</span></p>

			<p><span style="color:#ffffff">server.listen(8000);<br />
			console.log('サーバー起動中...')</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<h2>EJS</h2>

<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
	<thead>
		<tr>
			<th scope="col" style="background-color:#ffffff"><span style="font-size:16px">test.ejs</span></th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff"><html><br />
			&nbsp; &nbsp; <head><br />
			&nbsp; &nbsp; &nbsp; &nbsp; <meta charset='utf-8'><br />
			&nbsp; &nbsp; </head><br />
			&nbsp; &nbsp; <body></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; <p></span><span style="color:#f1c40f"><%-</span><span style="color:#ffffff"> val1 </span><span style="color:#f1c40f">%></span><span style="color:#ffffff"></p></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; <p></span><span style="color:#f1c40f"><%=</span><span style="color:#ffffff"> val1 </span><span style="color:#f1c40f">%></span><span style="color:#ffffff"></p></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#f1c40f"><%</span><span style="color:#ffffff"> for(var i=0; i<5; i++){ </span><span style="color:#f1c40f">%></span><br />
			<span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p></span><span style="color:#f1c40f"><%=</span><span style="color:#ffffff"> i </span><span style="color:#f1c40f">%></span><span style="color:#ffffff"></p><br />
			&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#f1c40f"><%</span><span style="color:#ffffff"> } </span><span style="color:#f1c40f">%></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; </body><br />
			</html></span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<h2>起動</h2>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff">node server.js</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<h2>出力結果</h2>

<p><img alt="" src="/uploadImage/20181215231116_3757124.png" /></p>