2013-08-05 16 views
2

我目前正在使用Express並嘗試解決(我相信應該是的)一個小問題。通過結構化的快遞應用服務html和javascript

我有以下目錄結構:

|-config 
    |---config.js 
    |---routes.js 
    |-server.js 
    |-scripts 
    |---controllers 
    |------controllers.js 
    |---directives 
    |---filters 
    |---services 
    |---templates 
    |---app.js 
    |-views 
    |---index.html 

我server.js

var express = require('express'); 
var app = express(); 

require('./config/config.js')(app); 
require('./config/routes.js')(app); 

app.listen(7777); 

我config.js

module.exports = function(app){ 
    app.set('views', __dirname + '../views'); 
    app.engine('html', require('ejs').renderFile); 
} 

我routes.js

module.exports = function(app, express){ 

    app.get('/', function(reg, res){ 
     res.render('index.html') 
    }) 

    app.use(function(err, req, res, next){ 
     console.error(err.stack); 
     res.send(500, 'Something broke!'); 
    }); 
} 

最後我的index.html

<html lang="en"> 
<head> 
    <title></title> 
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js'> 
    </script> 
</head> 
<body> 
    Hello World!!! 
</body> 
</html> 

當我訪問本地主機:7000/

我得到

Error: Failed to lookup view "index.html" 
    at Function.app.render (/Users/abe/github/leap-motion-signature-recognition/node_modules/express/lib/application.js:494:17) 
    at ServerResponse.res.render (/Users/abe/github/leap-motion-signature-recognition/node_modules/express/lib/response.js:756:7) 
    at /Users/abe/github/leap-motion-signature-recognition/config/routes.js:7:13 
    at callbacks (/Users/abe/github/leap-motion-signature-recognition/node_modules/express/lib/router/index.js:161:37) 
    at param (/Users/abe/github/leap-motion-signature-recognition/node_modules/express/lib/router/index.js:135:11) 
    at pass (/Users/abe/github/leap-motion-signature-recognition/node_modules/express/lib/router/index.js:142:5) 
    at Router._dispatch (/Users/abe/github/leap-motion-signature-recognition/node_modules/express/lib/router/index.js:170:5) 
    at Object.router (/Users/abe/github/leap-motion-signature-recognition/node_modules/express/lib/router/index.js:33:10) 
    at next (/Users/abe/github/leap-motion-signature-recognition/node_modules/express/node_modules/connect/lib/proto.js:190:15) 
    at Object.expressInit [as handle] (/Users/abe/github/leap-motion-signature-recognition/node_modules/express/lib/middleware.js:30:5) 

這是爲什麼? __dirNameset不應該掛鉤views\index.html?其次,我打算使用此服務器來支持包含許多JavaScript文件的Angular JS應用程序。 Rails資產管道的快速答案是什麼?我怎樣才能毫不費力地包含整個目錄,而不需要明確填寫script標籤,並且儘可能地縮短部署時間?

+0

變化'__dirname +」 ../ views''到'__dirname +'/../ views''。 – mak

+0

您可以使用[靜態中間件](http://expressjs.com/api.html#app.use)從目錄中提供靜態文件。 – mak

+0

您可以使用[exprses-uglify](https://github.com/ncrohn/express-uglify)中間件來縮小您的JavaScript文件。 – mak

回答

2

__dirname沒有結尾斜槓,所以您應該將__dirname + '../views'更改爲__dirname + '/../views'

您可以從目錄提供靜態文件與static middleware

app.use(express.static(__dirname + '/scripts')); 

express-uglify可以壓縮JavaScript文件:

var expressUglify = require('express-uglify'); 
app.use(expressUglify.middleware({ src: __dirname + '/scripts' })); 
相關問題