2016-07-15 16 views
0

我正在使用Express & Angular构建多页应用程序。我建立了我的文件夹结构是这样的:使用Express服务页面并允许文件包括

project 
|- server.js (The webserver) 
|- routes.js (The routes to serve the html pages) 
| |- node_modules 
| |- config 
| |- controllers (The routes for the API) 
| |- models (The SQL models - Sequelizer 
| |- client 
|  |- client.js (AngularJS main module) 
|  |- pages (Splitting the pages into Angular SPA) 
|   |- pageFolder (One for each page) 
|   |- page.html (The template for this SPA) 
|   |- page.js (The AngularJS controller) 
|   |- views (The views for the page) 

我已经做了广泛的研究,这似乎是一个很舒服的方法来组织一切。我的代码中的API部分与客户端上的AngularJS模块一样有魅力。我正在努力将两者联系起来。

在我routes.js文件,我服了已经被请求的特定页面索引文件 - 例如:

var express = require("express"); 
var router = express.Router(); 

router.get("/", function(request, response){ 
    response.sendFile("home.html",{ 
     root: "client/pages/home" 
    }); 
}); 

router.get("/otherPage", function(request, response){ 
    response.sendFile("otherPage.html",{ 
     root: "client/pages/otherPage" 
    }); 
}); 


module.exports = router; 

这是可以正常使用的文件,直到我开始尝试到AngularJS模块集成到网页。我建立了一个到node_modules文件夹的静态路由/resources,这使我可以在每个页面中包含AngularJS和其他库。该AngularJS控制器,然而,被放置在每个页面的文件夹,而不是在一个集中的文件夹controllers,当我尝试包括像这样的例子控制器:

<script src="home.js"></script> 

服务器处理请求的http://website.com/home.js我可以理解,但我不知道如何让页面包含在其文件夹中的文件?

任何建议,将不胜感激:)

回答

1

你可以试试这个:

app.use(express.static('folder1')); 
app.use('/folder2', express.static('folder2')); 

router.get("/otherPage", function(request, response){ 
    response.sendFile(__dirname + "/otherPage.html"); 
}); 

这样,website.com/folder2的任期将从文件夹2文件,你可以仅仅通过website.com获得它们/ folder2/file,当你访问website.com/otherPage时,它会给你/otherPage.html和类似的东西。

+0

我必须使用哪个文件夹作'folder1'和'folder2'? – nickcorin

+0

如果你有更多的资产以静态的方式提供服务,你将为首发者提供pageFolder,你应该以这种方式为他们提供服务。我对角度了解不多,所以我无法再提供帮助。 – ardilgulez

相关问题