2015-11-07 53 views
0

我有一個問題,試圖設置一個快速+角度的應用程序。Experess.js + angularjs單獨路由

我可以加載資產文件到它。

我會在這裏發表我的代碼從快遞和html文件。據我看到,Express只返回index.html文件,沒有其他任何東西,使所有的路線返回該文件。

這裏是快遞代碼:

//server.js 

// set up ======================== 
var express = require('express'), 
    bodyParser = require('body-parser'), 
    morgan = require('morgan'), 
    path = require('path'), 
    app = express(), 
    mongoose = require('mongoose'), 
    jwt = require('jsonwebtoken'), // used to create, sign, and verify tokens 
    config = require('./config'), // get our config file 
    User = require('./model/users'), //get our mongo model 
    routes = require('./middleware/routes'); 

// configuration ================= 
//var port = process.env.PORT || 8080; 
app.use(express.static('/client')); 
app.use(morgan('dev')); 
app.use(bodyParser.urlencoded({'extended':'true'}));   
app.use(bodyParser.json());          
app.use(bodyParser.json({ type: 'application/json' })); 
mongoose.connect(config.database); 
app.set('superPin', config.secret); 
// start server ================= 
// 
app.listen(8080); 
console.log('App strated and is listening on the port 8080'); 
// 
app.get('/checkServer', function(request,response){ 
    response.send('Works fine. Server started at default get routing to check if server runns.'); 
}); 
// 
app.get('*',function(request,response){ 
    response.sendFile(path.resolve('../client/view/index.html')); 
}); 
// 
//exports = module.exports = app; 

,這裏是我的index.html文件:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>My </title> 
    </head> 
    <body ng-app="pmt"> 
    <div ng-view></div> 
    <script src="client/assets/vendor/angular/angular.min.js"></script> 
    <script src="client/assets/vendor/angular-bootstrap/ui-bootstrap.min.js"></script> 
    <script src="client/assets/vendor/angular-message/angular-message.min.js"></script> 
    <script src="client/assets/vendor/angular-touch/angular-touch.min.js"></script> 
    <script src="client/assets/vendor/angular-ui-router/angular-ui-router.min.js"></script> 
    <script src="client/assets/vendor/angular-touch/angular-touch.min.js"></script> 
    <script src="client/assets/vendor/moment/min/moment.min.js"></script> 
    <script src="client/controller/routes.js"></script> 
    </body> 
</html> 

我會從我的文件夾結構現在添加打印屏幕: folder_tree_of_the_App

這裏是回購的鏈接:https://bitbucket.org/cojok/pmt/

我看到了其他相關帖子,但我無法找到我做錯了什麼。

我在瀏覽器中收到此錯誤enter image description here

+1

你究竟想要做什麼?你想要服務器端路由還是要角路由? –

+0

不能得到你在做什麼錯誤,你需要幫助? – prasun

+0

我正在嘗試使角度路由,但我無法加載我需要的文件。我總是得到這個愚蠢的錯誤,未捕獲的SyntaxError:意外的標記 cojok

回答

0

據我理解你的問題是你無法弄清楚如何在視圖鏈接文件或如何設置目錄文件的路線,所以在這裏你可以這樣做,因爲 添加目錄,如

app.use(express.static(path.join(__dirname, 'scripts'))); 
app.use(express.static(path.join(__dirname, 'Content'))); 
app.use(express.static(path.join(__dirname, 'Angular'))); 
app.use(express.static(path.join(__dirname, 'Views/Main'))); 
app.use(express.static(path.join(__dirname, 'Views/Authentication'))); 

至於內容,角度和腳本是根目錄和參考jQuery的文件作爲

<script src="angular-resource.js"></script> //in angular folder on root 
<script src="angular-ui-router.js"></script> //in script folder on root 
<script src="/Controllers/MainController.js"></script> // in angular folder with child directories 
0

我發現我的錯誤,或者這可能只是一個修復。但實際上改變這種

app.use(express.static('/client')); 
app.get('*',function(request,response){ 
    response.sendFile(path.resolve('../client/view/index.html')); 
}); 

這個

app.use('/client',express.static('../client/')); 
app.get('/',function(request,response){ 
    response.sendFile(path.resolve('../client/view/index.html')); 
}); 

解決了我的問題。

Thnx