###前提・実現したいこと
node.jsを勉強中です。
cssの読み込みがうまくいかずに困っています。
###該当のソースコード
const http = require('http'); const fs = require('fs'); const ejs = require('ejs'); const url = require('url'); const index_page = fs.readFileSync('./index.ejs', 'utf8'); const other_page = fs.readFileSync('./other.ejs', 'utf8'); const style_css = fs.readFileSync('./style.css', 'utf8'); var server = http.createServer(getFromClient); server.listen(3000); console.log('Server start!'); function getFromClient(request, response){ var url_parts = url.parse(request.url); switch (url_parts.pathname) { case '/': var content = ejs.render(index_page, { title:"Index page", content:"これはサンプルページです。", }); response.writeHead(200, {'Content-Type': 'text/html'}); response.write(content); response.end(); break; case '/style.css': response.writeHead(200, {'Content-Type': 'text/html'}); response.write(style_css); response.end(); break; default: response.writeHead(200, {'Content-Type': 'text/html'}); response.end('no page...'); break; } }
<!DOCTYPE html> <html lang="ja"> <head> <link type= "text/css" href= "./style.css" rel= "stylesheet"> <meta http-equiv="content-Type" content="text/html; charset=UTF-8"> <title><%= title %></title> </head> <body> <head> <h1><%= title %></h1> </head> <div role="main"> <p><%= content %></p> </div> </body> </html>
h1 { font-size: 60pt; color: #eee; text-align: right; margin: 0px; } body { font-size: 12pt; color: #999; margin: 5px; } p { font-size: 14pt; margin: 0px 20px; color: red; }
###補足情報(言語/FW/ツール等のバージョンなど)
ファイルは全て同じ階層に存在しています。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/11 02:32
2017/10/11 06:58