EJSで作ったテンプレートを表示させたいのですが、エラー?的なのが出てしまってページを見ることができません。
EJSはちゃんとインストールしました。
npm install -g ejs
エラー内容
PS C:\Users\---ユーザー名---\Desktop\作品\NODE-APP> node sample4.js module.js:549 throw err; ^ Error: Cannot find module 'ejs' at Function.Module._resolveFilename (module.js:547:15) at Function.Module._load (module.js:474:25) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object.<anonymous> (C:\Users\---ユーザー名---\Desktop\作品\NODE-APP\sample4.js:3:13) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3)
ファイル指定は間違っていません。
const http = require('http'); const fs = require('fs'); const ejs = require('ejs'); const index_page = fs.readFileSync**('./index.ejs','utf8');** var server = http.createServer(getFromClient); server.listen(3000); console.log('Server start'); // ここまでがメインプログラム============= // createServerの処理 function getFromClient(request,response){ var content = ejs.render(index_page); response.writeHead(200, {'Content-Tyoe': 'text/html'}); response.write(content); response.end(); }
<!DOCTYPE html> <html lang="ja"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Index</title> <style> h1{ font-size: 60pt; color: #eee; text-align: right; margin: 0px; } body{ font-size: 14pt; color: #999; margin: 5px; } </style> </head> <body> <head> <h1>Index</h1> </head> <div role="main"> <p>This isIndex Page.</p> <p>これは、ejsを使ったwebサイトです。</p> </div> </body> </html>
簡単ではありますが、よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。