nativescript和MongoDB之间的连接(mongoose)

时间:2016-07-17 19:20:11

标签: mongodb mongoose nativescript

我是移动开发领域的新手,现在正试图了解一些基本的东西。 我做了一个简单的登录nativescript应用程序,并从后端端做了mongoose(MongoDb)和express的登录逻辑。但现在我不知道如何继续......我如何连接后端和应用程序?

提前谢谢你, 埃米尔

1 个答案:

答案 0 :(得分:1)

你需要从你的后端公开一个API,我假设你已经完成了这个(或者可以找到它 - 它有很好的记录)。

因此,从客户端{N},您需要访问API,调用您需要的任何端点。如果您使用的是JWT类型方法,则应该使用nativescript中的http模块,该模块可能如下所示:

var http = require("http");
var result;

http.request({
    url: "https://myBackend.org/api/post",
    method: "POST",
    headers: { "Content-Type": "application/json" },
    content: JSON.stringify({ username: "ValueOne", password: "ValueTwo" })
}).then(function (response) {
    result = response.content.toJSON();
    console.log(result); //result.message would have the clients auth token
}, function (e) {
    // console.log("Error occurred " + e);
});

然后,您可以使用应用程序设置模块存储令牌(在持久存储中),并将其添加到对不同API端点的任何请求的标头中,以作为经过身份验证的用户与您的后端进行交互。

或者,您可以使用其中一个云后端SDK,例如: Azure Mobile ServicesFirebase让您的生活更轻松。