【发布时间】:2018-02-17 02:02:56
【问题描述】:
我尝试了解 API 优先架构以应用于 Web 应用程序,并在此处找到了一些信息,Advantages of a separate REST backend API?
下面我附上一个例子,以便更好地回答问题。 我开始使用 Angular.js 作为前端。 Node.js、Express.js 用于服务器。
文件夹结构
|-- server.js
|-- app
|-- models
|-- views
|-- index.html
|-- about.html
|-- contact.html
|-- controllers
|-- app.js
|-- node-modules
app 文件夹是 Angular 应用的 MVC 结构。所有 POST GET PUT DELETE、MongoDB、Redis 连接都在 server.js 中。但是当服务器文件变大时,组织这些业务逻辑的最佳方式是什么?
server.js 上没有设置视图引擎。如果我在 express 中创建“routes”文件夹,这是否实际执行 API 优先概念?比方说,当用户单击按钮直接从浏览器提交表单时,在这种情况下,服务器是否负责表示层?
index.html
<form method='POST' action='/register'>
// some code here..
<button type='submit'>submit</button></form>
server.js
var rootPath = './app/views'
router.post('/register' (req, res) =>{
// some code here...
res.sendFile( rootPath + 'index.html');
});
提前致谢。
【问题讨论】:
标签: angularjs node.js rest api express