如何使托管的nodejs网站在cordova上运行?

时间:2020-07-19 22:29:31

标签: ios node.js cordova hosted

我有一个使用NodeJS来管理数据库的网站(HTML5游戏),我正尝试使用Cordova将其移植到android和IOS上,但是我不知道如何使用Cordova来使Cordova加载该网站。 URL,由于所有服务器端内容,我需要将其加载到URL中。有人对此有任何建议吗?

我看过无数的文章/帖子,但是每次我通过单击index.HTML对其进行测试时,HTML都不会重定向/显示我的网站。

我尝试了几个插件,甚至只是使用简单的javascript来加载网站,但没有任何效果。

打开index.html时出现以下错误

Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”).

我的config.xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.hellocordova" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloCordova</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="http://mysite.us*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>
<cordova>
    <access origin="http://mysite.us*"/>
</cordova>

我的index.js

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.receivedEvent('deviceready');
            window.location="http://mysite.us";
    },

    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

app.initialize();

1 个答案:

答案 0 :(得分:1)

这是我使用的代码:

  <plugin name="cordova-plugin-whitelist" source="npm" />
  <allow-navigation href="https://example.org/*" />
  <allow-intent href="https://example.org/*" />
  <access origin="https://example.org/*" />

不需要在平台标记中,显然您需要用自己的网址替换

如果需要选择文件夹或域中的所有内容,则可以使用通配符:*(如上图所示)

编辑:

请记住,您还需要将白名单添加到您的Web应用程序加载的所有资源中,例如: 如果通过CDN加载jQuery,则需要将该URL或CDN的整个域名列入白名单。

还请记住,如果首先将外部URL列入黑名单,则是为了防止Xss注入。如果您将过多的网址列入白名单,则人们可能会从您已列入白名单的源中加载脚本,从而通过您的应用欺骗用户。

编辑2:

您也可以直接使用iframe而不是window.location

<iframe src="https://example.com/nodejs-webapp/" style="border: 0; width: 100%; height: 100%">Your browser doesn't support iFrames.</iframe>

这放在head标签中

<style>body{margin:0;}</style>