The other day, I wrote The story of building an OSS WebDB system called Exment with the combination of Lightsail + Docker.
After that, I also wrote Article that combines Exment and MF cloud invoice with API.
So, you forgot to write important things in both articles.
It works with Postman, but it doesn't work with Node.js. Why ... Yes, CORS ...
So, Docker-compose that operates Extension is here, but the web server is running on nginx.
That's why I added the CORS settings for nginx.
nginx.conf
server {
listen 8080;
server_name _;
root /var/www/exment/public;
index index.php index.html;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin '*';
add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE';
add_header Access-Control-Allow-Headers 'Origin, Authorization, Accept, Content-Type';
add_header Access-Control-Max-Age 3600;
add_header Content-Type 'text/plain charset=UTF-8';
add_header Content-Length 0;
return 204;
}
try_files $uri /index.php?$query_string;
}
location ~ \.php$ {
add_header Access-Control-Allow-Origin '*' always;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
--Reference: CORS setting definitive edition for setting up API server
Recommended Posts