简体   繁体   English

我如何将Node.js实现为Ionic / Angular应用程序?

[英]How would I implement Node.js into an Ionic / Angular application?

I've currently got a base "tabs" Ionic / Angular application. 我目前有一个基础“标签”Ionic / Angular应用程序。

ionic start testproject tabs

I've gone ahead and done an npm install to get some of the basic node modules in the project directory. 我已经完成了npm install以获得项目目录中的一些基本节点模块。

I'm a little confused about how to use Angular and Express together, and how to set up the node / server end of things. 我对如何一起使用Angular和Express以及如何设置节点/服务器端的事情感到有些困惑。 I've tried looking at a bunch of tutorials, and find myself getting a little lost in the mix, so I'm hoping someone will have some resources that might help push me in the right direction. 我已经尝试过看一堆教程,发现自己在混合中有点迷失,所以我希望有人会有一些资源可能会帮助我朝着正确的方向前进。

Because Angular and Express both do MVC / MV* -- It's starting to get really confusing as to what does what. 因为Angular和Express都做MVC / MV * - 它开始变得令人困惑的是什么做了什么。 I'm confused on setup and how to get them to talk together. 我对设置感到困惑,以及如何让他们一起聊天。

Please let me know what other information I can give, as I'm not sure what else. 请告诉我我能提供的其他信息,因为我不确定还有什么。 (The project is pretty bare bones.) (该项目非常简单。)

Thank you so much! 非常感谢!

Standard Angular Web App 标准角度Web应用程序

When building a MEAN web app you should start by creating an Express application with Express-Generator these commands (assuming you have node and express installed global) 在构建MEAN Web应用程序时,您应该首先使用Express-Generator创建Express应用程序这些命令(假设您有节点和express已安装全局)

$ express myapp $ express myapp

to build the app 构建应用程序

$ cd myapp $ cd myapp

to enter the app 进入应用程序

$ npm install $ npm安装

to install the dependencies from the package.json file package.json文件安装依赖项

File Structure 文件结构

.
├── app.js
├── bin
│   └── www
├── models
│   └── home.js
├── node_modules
├── package.json
├── public // your Angular app goes here
├── README.md
├── routes
│   ├── home.js
│   └── users.js
└── views

So above we can see the basic structure of the app. 所以上面我们可以看到应用程序的基本结构。 You should have something similar after running Express generator. 运行Express生成器后应该有类似的东西。 You will take your Angular app and put it into the public folder and run the server with 您将获取Angular应用程序并将其放入公用文件夹并运行服务器

$ DEBUG=myapp npm start $ DEBUG = myapp npm start

Ionic 离子的

With an Ionic app you want an app for a phone that someone will, because of that you wont need to add the app to the public folder here. 使用Ionic应用程序,您需要某个人的手机应用程序,因为您不需要在此处将应用程序添加到公共文件夹中。 What you will use Express for is to create an api for your app to call. 您将使用Express来创建一个api供您的应用程序调用。 You can create a simple web api with a server.js file that you can run with 您可以使用可以运行的server.js文件创建一个简单的Web api

$ node server.js $ node server.js

In your Angular Factories and Services you can make $http calls directly to your api. 在您的Angular Factories and Services中,您可以直接向您的api拨打$http电话。

Update 更新

Currently working on a boilerplate to split back and front end of a mean app, this means that you will be able to serve the exact same backend to you android app, ios app and your web app. 目前正在使用样板来拆分平均应用程序的后端和前端,这意味着您将能够为您的Android应用程序,ios应用程序和您的Web应用程序提供完全相同的后端。

Its a work in progress but feel free to check it out for ideas or to start your own separate front and back end MEAN stack. 它是一项正在进行的工作,但随时可以查看它的想法或开始你自己的单独的前端和后端MEAN堆栈。 https://github.com/joeLloyd/MEANBoilerPlate https://github.com/joeLloyd/MEANBoilerPlate

It seems you were confused by the term MVC and its meaning. 似乎你对术语MVC及其含义感到困惑。 MVC (Model, View, Controller) is just a general pattern for the structure of an application. MVC(模型,视图,控制器)只是应用程序结构的一般模式。

When writing a web application with angular, you are actually writing two applications (in most cases): 使用angular编写Web应用程序时,实际上是在编写两个应用程序(在大多数情况下):

Your frontend application, the one that runs in your browser, using HTML, JS and CSS (and frameworks like angular on top of them), displaying data to the user and allowing them to interact with it. 您的前端应用程序,即在您的浏览器中运行的应用程序,使用HTML,JS和CSS(以及它们之上的角度等框架),向用户显示数据并允许它们与之交互。 And the backend application, the one that runs on your server (for example on node or Spring or RubyOnRails...), storing the data, serving it and calculating the business logic. 后端应用程序,即在您的服务器上运行的应用程序(例如在节点或Spring或RubyOnRails上运行...),存储数据,提供数据并计算业务逻辑。

Those two applications can both independently be structured using an MVC pattern. 这两个应用程序都可以使用MVC模式独立构建。 However that does not influence how they work together - they are completely independent and communicate using the HTTP protocol (using AJAX on the frontend). 然而,这并不影响它们如何协同工作 - 它们完全独立并使用HTTP协议进行通信(在前端使用AJAX)。 So to your angular frontend application, it does not matter if the backend application runs on Node or anything else. 因此,对于角度前端应用程序,后端应用程序是否在Node或其他任何设备上运行并不重要。

Just to explain the names int hat context: 只是为了解释名称int hat context:

Node is a backend framework, running the server, doing business logic and talking to the database. Node是一个后端框架,运行服务器,执行业务逻辑并与数据库通信。 Express is a module for Node that makes it easier to write HTTP APIs in Node. Express是Node的一个模块,可以更轻松地在Node中编写HTTP API。

Ionic and Angular are frontend frameworks. Ionic和Angular是前端框架。 They allow you to create frontend applications more easily. 它们允许您更轻松地创建前端应用程序。

I hope that helped a bit, its a pretty huge topic and its not really possible to explain all of it in a reasonably sized StackOverflow answer. 我希望这有点帮助,它是一个非常庞大的主题,它不可能在一个合理大小的StackOverflow答案中解释所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM