2016-11-22 42 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