Difference between revisions of "Setup Precondition"

From MSX - Wiki
Jump to navigation Jump to search
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
You need a local or public HTTP server (e.g. Apache, nginx, Microsoft IIS, Node.js, etc.) to host your JSON files. Alternatively, you can use your NAS (Network Attached Storage) server or device that supports web server functionality. The HTTP server must support '''CORS''' (Cross-Origin Resource Sharing) to ensure that the JSON files can be loaded from each platform. This should not be a problem, because almost any HTTP server can be configured to support CORS. However, depending on the used HTTP server, the CORS support is configured differently. Please search the web for how it is done for your server. The next examples show how a basic CORS support can look like.
 
You need a local or public HTTP server (e.g. Apache, nginx, Microsoft IIS, Node.js, etc.) to host your JSON files. Alternatively, you can use your NAS (Network Attached Storage) server or device that supports web server functionality. The HTTP server must support '''CORS''' (Cross-Origin Resource Sharing) to ensure that the JSON files can be loaded from each platform. This should not be a problem, because almost any HTTP server can be configured to support CORS. However, depending on the used HTTP server, the CORS support is configured differently. Please search the web for how it is done for your server. The next examples show how a basic CORS support can look like.
  
== via Apache and the <code>.htaccess</code> file ==
+
== CORS via Apache and the <code>.htaccess</code> file ==
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
Header set Access-Control-Allow-Origin "*"
 
Header set Access-Control-Allow-Origin "*"
Line 9: Line 9:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== via PHP ==
+
== CORS via PHP ==
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
 
<?php
 
<?php
Line 18: Line 18:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== via Java and the <code>HttpServlet</code> interface ==
+
== CORS via Java and the <code>HttpServlet</code> interface ==
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Line 28: Line 28:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== via Node.js and the <code>http</code> library ==
+
== CORS via Node.js and the <code>http</code> library ==
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
 
const server = http.createServer((req, res) => {
 
const server = http.createServer((req, res) => {
Line 38: Line 38:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== via Node.js and the <code>http-server</code> package ==
+
== CORS via Node.js and the <code>http-server</code> package ==
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
http-server --cors
 
http-server --cors
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== via Apache Tomcat and the <code>web.xml</code> file ==
+
== CORS via Apache Tomcat and the <code>web.xml</code> file ==
 
<syntaxhighlight lang="xml">
 
<syntaxhighlight lang="xml">
 
<!-- ... -->
 
<!-- ... -->
Line 69: Line 69:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== via Microsoft IIS and the <code>web.config</code> file ==
+
== CORS via Microsoft IIS and the <code>web.config</code> file ==
 
<syntaxhighlight lang="xml">
 
<syntaxhighlight lang="xml">
 
<!-- ... -->
 
<!-- ... -->
Line 90: Line 90:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
For more information about CORS, please visit https://en.wikipedia.org/wiki/Cross-origin_resource_sharing.
+
== CORS via nginx and the <code>nginx.conf</code> file ==
 +
<syntaxhighlight lang="nginx">
 +
http {
 +
    server {
 +
        #...
 +
        add_header 'Access-Control-Allow-Origin' '*';
 +
        add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept';
 +
        add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
 +
        #...
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 
 +
== More information about CORS ==
 +
For more information about CORS, please visit: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing.
 +
 
 +
== See also ==
 +
* [[Start Object]]
 +
* [[Setup Start Parameter]]

Latest revision as of 09:48, 13 May 2022

You need a local or public HTTP server (e.g. Apache, nginx, Microsoft IIS, Node.js, etc.) to host your JSON files. Alternatively, you can use your NAS (Network Attached Storage) server or device that supports web server functionality. The HTTP server must support CORS (Cross-Origin Resource Sharing) to ensure that the JSON files can be loaded from each platform. This should not be a problem, because almost any HTTP server can be configured to support CORS. However, depending on the used HTTP server, the CORS support is configured differently. Please search the web for how it is done for your server. The next examples show how a basic CORS support can look like.

CORS via Apache and the .htaccess file[edit]

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, Content-Type, Accept"
Header set Access-Control-Allow-Methods "GET, OPTIONS"
#...

CORS via PHP[edit]

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept');
header('Access-Control-Allow-Methods: GET, OPTIONS');
//...

CORS via Java and the HttpServlet interface[edit]

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setHeader("Access-Control-Allow-Origin", "*");
    resp.setHeader("Access-Control-Allow-Headers", "Origin, Content-Type, Accept");
    resp.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
    //...        
}

CORS via Node.js and the http library[edit]

const server = http.createServer((req, res) => {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Headers", "Origin, Content-Type, Accept");
    res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
    //...
});

CORS via Node.js and the http-server package[edit]

http-server --cors

CORS via Apache Tomcat and the web.xml file[edit]

<!-- ... -->
<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>Origin, Content-Type, Accept</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET, OPTIONS</param-value>
    </init-param>    
</filter>
<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ... -->

CORS via Microsoft IIS and the web.config file[edit]

<!-- ... -->
<system.webServer>
    <cors enabled="true">
        <add origin="*">
            <allowHeaders>
                <add header="Origin" />
                <add header="Content-Type" />
                <add header="Accept" />
            </allowHeaders>
            <allowMethods>
                <add method="GET" />
                <add method="OPTIONS" />
            </allowMethods>
        </add>
    </cors>
</system.webServer>  
<!-- ... -->

CORS via nginx and the nginx.conf file[edit]

http {
    server {
        #...
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept';
        add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';	
        #...
    }
}

More information about CORS[edit]

For more information about CORS, please visit: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing.

See also[edit]