| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- /**
- * 本地开发服务器,解决跨域问题
- *
- * 使用方法:node server.js
- * 启动后访问:http://localhost:3000/yuyue.html?test=1
- */
- const http = require('http')
- const fs = require('fs')
- const path = require('path')
- const url = require('url')
- const PORT = 3003
- const API_HOST = '61.164.207.46'
- const API_PORT = 8000
- const MIME = {
- '.html': 'text/html;charset=utf-8',
- '.js': 'application/javascript;charset=utf-8',
- '.css': 'text/css;charset=utf-8',
- '.json': 'application/json;charset=utf-8',
- '.png': 'image/png',
- '.jpg': 'image/jpeg',
- '.gif': 'image/gif',
- '.svg': 'image/svg+xml',
- '.ico': 'image/x-icon'
- }
- // 静态文件服务
- function serveStatic(req, res) {
- var pathname = url.parse(req.url).pathname
- if (pathname === '/') pathname = '/yuyue.html'
- var filePath = path.join(__dirname, pathname)
- var ext = path.extname(filePath)
- fs.readFile(filePath, function(err, data) {
- if (err) {
- res.writeHead(404, { 'Content-Type': 'text/plain' })
- res.end('Not Found')
- return
- }
- res.writeHead(200, {
- 'Content-Type': MIME[ext] || 'application/octet-stream',
- // 允许页面被 iframe 嵌入
- 'X-Frame-Options': 'SAMEORIGIN'
- })
- res.end(data)
- })
- }
- // 代理 API 请求
- function proxyApi(req, res) {
- var chunks = []
- req.on('data', function(chunk) { chunks.push(chunk) })
- req.on('end', function() {
- var body = Buffer.concat(chunks)
- // 从请求的 Host 头中获取域名和端口号
- var host = req.headers.host || (API_HOST + ':' + API_PORT)
- var hostParts = host.split(':')
- var targetHost = hostParts[0]
- var targetPort = hostParts[1] || '80'
- var options = {
- hostname: targetHost,
- port: targetPort,
- path: '/yos/rest/index',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json;charset=utf-8',
- 'Content-Length': body.length
- }
- }
- var proxyReq = http.request(options, function(proxyRes) {
- var data = []
- proxyRes.on('data', function(chunk) { data.push(chunk) })
- proxyRes.on('end', function() {
- // 返回响应,添加 CORS 头
- res.writeHead(proxyRes.statusCode, {
- 'Content-Type': 'application/json;charset=utf-8',
- 'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Methods': 'POST, OPTIONS',
- 'Access-Control-Allow-Headers': 'Content-Type'
- })
- res.end(Buffer.concat(data))
- })
- })
- proxyReq.on('error', function(err) {
- res.writeHead(500, { 'Content-Type': 'application/json;charset=utf-8' })
- res.end(JSON.stringify({ code: -1, data: '代理请求失败: ' + err.message }))
- })
- proxyReq.write(body)
- proxyReq.end()
- })
- }
- var server = http.createServer(function(req, res) {
- var pathname = url.parse(req.url).pathname
- // 处理 OPTIONS 预检请求
- if (req.method === 'OPTIONS') {
- res.writeHead(204, {
- 'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Methods': 'POST, OPTIONS',
- 'Access-Control-Allow-Headers': 'Content-Type',
- 'Access-Control-Max-Age': 86400
- })
- res.end()
- return
- }
- // API 代理
- if (pathname === '/api/proxy') {
- proxyApi(req, res)
- return
- }
- // 静态文件
- serveStatic(req, res)
- })
- server.listen(PORT, '0.0.0.0', function() {
- console.log('========================================')
- console.log(' 本地开发服务器已启动')
- console.log(' 访问地址: http://localhost:' + PORT + '/yuyue.html')
- console.log(' 测试模式: http://localhost:' + PORT + '/yuyue.html?test=1')
- console.log(' 按 Ctrl+C 停止')
- console.log('========================================')
- })
|