2016-11-22 57 views
0

我是Node.js和Express的新手。我想創建一個基本的AngularJS應用程序,但我不知道從哪裏開始。這裏是我想實現的文件組織:創建基本應用程序 - MEAN

- public 
----- app 
---------- components 
----------------- component0 
----------------------- c0controller.js 
----------------------- c0.html 
----------------------- c0Service.js 
----------------- component1 
----------------------- c1controller.js 
----------------------- c1.html 
----------------------- c1Service.js 
---------- assets [...] 
----- index.html 
----- app.js 
----- module.js 
- node_modules [...] 
- server.js 
- route.js 

首先,是否可以這樣做?

基本上,index.html文件是我定義我的角度應用程序的ui-view的地方。

我的主要問題是,我不能找出如何設置在server.js我的節點服務器...這是我有但說實話,我不明白,每行...

// set up ======================== 
var express = require('express'); 
var app  = express();        // create our app w/ express 
var mongoose = require('mongoose');      // mongoose for mongodb 
var morgan = require('morgan');    // log requests to the console (express4) 
var bodyParser = require('body-parser'); // pull information from HTML POST (express4) 
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4) 
var routes = require('./routes.js'); 

// configuration ================= 

mongoose.connect('mongodb://localhost/test');  // connect to mongoDB database 

app.set('views', __dirname + '/public/app'); 
app.set('view engine', 'html'); 
app.use(express.static(__dirname + '/public'));     // set the static files location /public/img will be /img for users 
app.use(morgan('dev'));           // log every request to the console 
app.use(bodyParser.urlencoded({'extended':'true'}));   // parse application/x-www-form-urlencoded 
app.use(bodyParser.json());          // parse application/json 
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json 
app.use(methodOverride()); 

app.get('/', routes.index); 
app.get('*', routes.index); 

// listen (start app with node server.js) ====================================== 
app.listen(8080); 
console.log("App listening on port 8080"); 

而在routes.js我導出以下方法:

var exports = module.exports = {}; 

exports.index = function(req, res){ 
    res.render('index'); 
}; 

我已經安裝人員ejs,但我不知道該怎麼用它做什麼......我有點失去了任何幫助,將不勝感激; )

+0

你看過種子嗎?像:https://github.com/meanie/express-seed – Alan

+0

明天我會給它一個去吧謝謝你;) –

回答

0

只給你每行的一個簡短的說明....

你需要在你的項目中使用以下是圖書館...

// set up ======================== 
var express = require('express'); 
var app  = express();        // create our app w/ express 
var mongoose = require('mongoose');      // mongoose for mongodb 
var morgan = require('morgan');    // log requests to the console (express4) 
var bodyParser = require('body-parser'); // pull information from HTML POST (express4) 
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4) 

的路由變量設置爲導入/您的應用程序的routes.js文件中的必需對象。可能會有一個modules.export對象在routes.js文件中分配了一個routes對象,該對象將被導出到您的應用程序的其他部分。

var routes = require('./routes.js'); 

這是你連接到你的數據庫的地方。您正在使用貓鼬庫來幫助您爲模型創建模式。 mongoose的connect方法內的字符串是數據庫的位置('test')。在嘗試提供應用程序之前,您需要確保您正在運行mongodb。

// configuration =================  
mongoose.connect('mongodb://localhost/test');  // connect to mongoDB database 

這裏您將應用程序的視圖設置到公共目錄中的app目錄。在應用程序內部,您將能夠創建您的html文件。

app.set('views', __dirname + '/public/app'); 

在這裏你說明你的視圖引擎將是純html。

app.set('view engine', 'html'); 

您正在告訴表達您的所有靜態文件,如圖像將位於/ public目錄中。

app.use(express.static(__dirname + '/public'));     
//set the 
static files location /public/img will be /img for users 

以下是您需要幫助您進行應用程序開發的中間件。摩根是不言而喻的,因爲它有一個評論,bodyParser爲您解析響應的主體和關鍵HTTP請求對象......

app.use(morgan('dev'));           // log every request to the console 
app.use(bodyParser.urlencoded({'extended':'true'}));   // parse application/x-www-form-urlencoded 
app.use(bodyParser.json());          // parse application/json 
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json 
app.use(methodOverride()); 

這是你的路線。基本上,當你點擊路徑'/'時,route.index裏面的任何內容都會被執行......

app.get('/', routes.index); 
app.get('*', routes.index); 

下面指定端口8080作爲應用程序將偵聽傳入請求的端口。

// listen (start app with node server.js) ====================================== 
app.listen(8080); 
console.log("App listening on port 8080"); 
+0

非常感謝你的每一個細節!我幾乎瞭解所有我認爲的東西,但我不確定意見部分...現在,當我瀏覽localhost:8080時,我會自動重定向到localhost:8080 /#/ home。它意味着index.html文件被找到,並且app.js(我定義了角度路由的地方)中的代碼被執行了......但是home.html沒有被渲染......事實上,只要我有500個錯誤代碼...我需要什麼來呈現我想使用的templateUrl?謝謝你的時間! –

+0

#/ home在您的角度應用中定義,因爲角度可以配置爲使用客戶端路由。您的快速應用程序是服務器端渲染。 您需要更新角碼 – Alan