Node.js、HTMLで記述したプログラムが 'ReferenceError: document is not defined'エラーで動きません。
Node.js、HTMLで記述したプログラムが
'ReferenceError: document is not defined'エラーで動きません。
表題の件で質問です。
Node.js、HTMLを用いて
「習慣」「それに使った時間」を入力 → 週・月・年単位で予定消費時間を出力する
WEBアプリを作っているのですが、ターミナルでjsファイルを実行しようとすると下記のようなエラーが出てきてしまいます。
解決方法をご存じの方がいらっしゃいましたら、アドバイスをいただけませんでしょうか。
発生している問題・エラー
PS D:\code\Project1> node app.js
D:\code\Project1\app.js:12
const habit = document.habit;
^
ReferenceError: document is not defined
at Object.<anonymous> (D:\code\Project1\app.js:12:19)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
該当するソースコード
入力ページHTML
<!DOCTYPE html>
<head>
<title>Life Time Manager</title>
<link rel = "stylesheet" href="./stylesheet.css">
</head>
<body>
<label for="name">習慣</label>
<input type="text" id="habit" minlength="1" maxlength="10">
<label for="name">時間</label>
<input type="number" id="timeOnHabit" minlength="1" maxlength="10">
<form name="form1">
<select name="unit" size="1">
<option value="day">/日</option>
<option value="week">/週</option>
<option value="month">/月</option>
<option value="year">/年</option>
</select>
</form>
<br>
<a href="/views/result.ejs">
<button type="submit" id="submit" >確認してみる</button>
</a>
<script src = "app.js"></script>
</body>
</html>
出力ページHTML
<!DOCTYPE html>
<head>
<title>Life Time Manager</title>
<link rel = "stylesheet" href="./stylesheet.css">
</head>
<body>
<p>貴方は<%= habit %>をすることで</p>
<p>1日あたり およそ<%= T %>時間</p>
<p>1週間あたり およそ<%= T*7 %>時間</p>
<p>1ヵ月あたり およそ<%= T*30 %>時間</p>
<p>1年あたり およそ<%= T*365 %>時間</p>
<p>を費やしています。</p>
</body>
</html>
Javascript
const express = require('express');
const app = express();
app.get('/top', (req, res)=>{
res.render('top.ejs');
});
app.get('/result', (req, res)=>{
res.render('result.ejs');
});
const habit = document.habit;
const timeOnHabit = document.timeOnHabit;
const unit = document.form1.unit;
const T = (timeOnHabit, unit)=>{
if(unit = day){
return timeOnHabit;
}
if(unit = week){
return timeOnHabit/7;
}
if(unit = month){
return timeOnHabit/30;
}
if(unit = year){
return timeOnHabit/365;
}
};
app.listen(3000);
0 likes