2016-11-22 5 views
0

私はNode.jsとExpressも使い慣れています。基本的なAngularJSアプリケーションを作成したいと思いますが、どこから起動するのかはわかりません。達成したいファイル構成は次のとおりです。基本的なアプリケーションを作成する - 平均

- 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をのinstallerが、私はそれをどうするかわからないんだけど...私は任意の助けをいただければ幸いですので、少し迷ってしまいました。 )

+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ファイル内の必須オブジェクト。 routes.jsファイルにroutesオブジェクトが割り当てられたmodules.exportオブジェクトがあります。このオブジェクトは、アプリの他の部分にエクスポートされます。

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これはあなたのルートです...

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()); 

あなたの応答と要求のHTTPオブジェクトの本体を解析するために重要です。基本的にあなたがルート '/'を押すと、routes.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はレンダリングされません...実際には、エラーコード...私は使用したいtemplateUrlをレンダリングするために何が必要ですか?御時間ありがとうございます! –

+0

#角度の設定がクライアント側のルーティングを使用するように設定できるので、角度アプリで#/ homeが定義されています。エクスプレスアプリはサーバサイドレンダリングです。 角度コードを更新する必要があります – Alan

関連する問題