node.js+sokect.io+expressでお絵かき共有機能を作っております。
node.jsでjsファイルを起動させて、指定のhtmlファイルを動かしているのですが、
その際に画像やcssファイルは、apacheが起動していないからindex.htmlから読み込むことができません。
node.を使用する際は、読み込む必要があるファイルは必ず予め記述しておく必要がありますか?簡略化することは出来ますでしょうか?
もしくはapacheのように記述できる方法などありますでしょうか?(これが出来るのが一番望ましいです)
使うcssと画像ファイルの数だけ、以下のように長いコードになってしまいますので、省略できる方法を探しています。
javascript
1var express = require('express') 2 , http = require('http') 3 , app = express() 4 , server = http.createServer(app) 5 , io = require('socket.io').listen(server); 6 7server.listen(80); 8 9app.get('/', function (req, res) { 10 res.sendfile(__dirname + '/index.html'); 11}); 12 13//これ以下の記述をシンプルにしたい 14 15app.get('/css/reset.css', function (req, res) { 16 res.sendfile(__dirname + '/css/reset.css'); 17}); 18 19app.get('/css/bootstrap.min.css', function (req, res) { 20 res.sendfile(__dirname + '/css/bootstrap.min.css'); 21}); 22 23app.get('/css/common.css', function (req, res) { 24 res.sendfile(__dirname + '/css/common.css'); 25}); 26 27app.get('/img/top/1.png', function (req, res) { 28 res.sendfile(__dirname + '/img/top/1.png'); 29}); 30 31app.get('/img/top/2.png', function (req, res) { 32 res.sendfile(__dirname + '/img/top/2.png'); 33}); 34 35app.get('/img/top/3.png', function (req, res) { 36 res.sendfile(__dirname + '/img/top/3.png'); 37}); 38 39app.get('/img/top/4.png', function (req, res) { 40 res.sendfile(__dirname + '/img/top/4.png'); 41}); 42 43app.get('/img/top/5.png', function (req, res) { 44 res.sendfile(__dirname + '/img/top/5.png'); 45}); 46 47app.get('/img/top/6.png', function (req, res) { 48 res.sendfile(__dirname + '/img/top/6.png'); 49}); 50 51app.get('/img/top/7.png', function (req, res) { 52 res.sendfile(__dirname + '/img/top/7.png'); 53}); 54 55app.get('/img/top/8.png', function (req, res) { 56 res.sendfile(__dirname + '/img/top/8.png'); 57}); 58 59app.get('/img/top/9.png', function (req, res) { 60 res.sendfile(__dirname + '/img/top/9.png'); 61}); 62
コードか、参考サイトでご教示いただければ理解しやすいです。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/07/22 10:43