Javascript 应用程序避免使用服务器端语言

Javascript app avoid server-side languages

本文关键字:服务器端 语言 应用程序 Javascript      更新时间:2023-09-26

做一些实验来构建JavaScript应用程序,不支持任何服务器端语言,如php,python......只是带有网络数据库层API的JavaScript。使用 orchestrate.io,但HTML5 CORS需要严格的标头响应,如Access-Control-Allow-Origin。有没有办法构建一种应用程序步入 DbaaS?

例如,nginx配置为仅运行 www/index.html。我们需要通过 HTTP 使用 REST API 获取 .json 数据。这是我们的博客文章。JSON-P 无法发送 http 标头(?)。

谁知道呢?

设置:

恩金克斯

server {
    ...
    root /usr/local/www
    index index.html
    ...
}

索引.html

function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // Check if the XMLHttpRequest object has a "withCredentials" property.
    // "withCredentials" only exists on XMLHTTPRequest2 objects.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // Otherwise, check if XDomainRequest.
    // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // Otherwise, CORS is not supported by the browser.
    xhr = null;
  }
  return xhr;
}
var xhr = createCORSRequest('GET', url);
if (!xhr) {
    throw new Error('CORS not supported');
}
xhr.withCredentials = true;
var url = 'https://api.service.io/article/1';
var xhr = createCORSRequest('GET', url);
xhr.onload = function() {
    var responseText = xhr.responseText;
    console.log(responseText);
 // process the response.
};
xhr.onerror = function() {
    console.log('There was an error!');
};
xhr.send();

我需要发送基本的身份验证 HTTP 标头...就这样

Nginx代理模块可以做到这一点:它用普通XMLHTTPRequest捕获你的请求到你选择的某个本地(同源)路径,并将其代理到远程服务,添加服务所需的任何标头proxy_set_header指令。

例如,我只用几行就向 Mailgun 服务发出代理请求:

location = /mailgun-send
{
  proxy_pass  https://api.mailgun.net/v2/mg.inshaker.ru/messages;
  proxy_set_header  'Host' 'api.mailgun.net';
  proxy_set_header  'Authorization' 'Basic YXB3lzbWJlaTVq2N1ZjJ6NG1MDh5aDd0epOmtleS0zM4NjRseXVhNw==';
}

然后就$.ajax('/mailgun-send').