节点 js 文件流与 express 以及在哪里添加业务逻辑

Node js file flow with express and where to add business logic there

你能解释一下文件在 node js 中是如何用 express 流动的吗?例如,应用程序以 app.js 开始,然后转到路由的 index.js 文件等。在此流程中,在哪里添加业务逻辑以及所有内容如何连接?

Node.js 是服务器端 java 脚本语言。 Express 是 node.js 网络应用程序框架。

项目的基本目录结构可能如下所示。

- app/               // application content
----- index.html              
- node_modules/      // created by npm. holds our dependencies/packages
- package.json       // define all our node app and dependencies
- server.js          // entry point for application

您通常会提供某种类型的 index.html 文件作为您应用程序的入口点(在您的 server.js 文件中声明)。您还将在 server.js 文件中定义其他实现业务逻辑或提供其他内容的路由。

例如,如果我当前导航到 index.html(默认路由“/”)并单击一个应该从后端检索一些数据的按钮,我将在前端实现一些东西(AJAX call, Angular) 调用我的后端服务器功能。然后我的后端功能将处理我的请求并将响应发送回前端。

以下是一个非常基本的示例,说明如何 "glue" 将前端和后端结合在一起。

后端:

app = express();

app.get('/getData',function(res,req){
  /... code to get the data .../
});

Front-end:

$http.get('http://localhost:8080/getData').success(function(data){
 /... do what needs to be done at the front end to display data .../
});