【问题标题】:Node js file flow with express and where to add business logic there带有express的Node js文件流以及在哪里添加业务逻辑
【发布时间】:2018-03-07 00:49:52
【问题描述】:

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

【问题讨论】:

    标签: node.js project-structure


    【解决方案1】:

    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 调用, Angular)来调用我的后端服务器功能。然后我的后端功能会处理我的请求并将响应发送回前端。

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

    后端:

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

    前端:

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

    【讨论】:

      猜你喜欢
      • 2013-08-28
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 2011-06-01
      • 2011-08-02
      • 2011-08-02
      • 1970-01-01
      相关资源
      最近更新 更多