14.7 nginx跨域
编辑nginx文件 vi /usr/local/etc/nginx/nginx.conf
nginx配置如下,配置后需重启nginx nginx -s reload
location ~.*\.json { root html; add_header "Access-Control-Allow-Origin" "*"; }
前端
index.html
<script> fetch('http://localhost:1207/a.json', { method: 'get' }) .then(res => { return res.text() }).then(res => { console.log(res) }) </script>
后端
index.js
let express = require('express') let app = express() app.use(express.static(__dirname)) app.listen(3000)