美文网首页
2023-12-12- 🌦🌦nodejs做服务器和服务代理

2023-12-12- 🌦🌦nodejs做服务器和服务代理

作者: 沐深 | 来源:发表于2023-12-11 14:34 被阅读0次

最近遇到在 windows2008部署前端项目的问题,tomact 和 ng 都不太合适小项目,而且配置比较复杂,使用 node搞个服务器吧
把打包好的静态文件放在 dist 文件夹下,目录如下:
-dist
--assets
-- index.html

服务器 express

const path = require('path');
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const PORT = 3001;

const STATIC = path.resolve(__dirname, 'dist');
const INDEX = path.resolve(STATIC, 'index.html');

const app = express();
// Static content
app.use(express.static(STATIC));


// All GET request handled by INDEX file
app.get('*', function (req, res) {
  res.sendFile(INDEX);
});

// Start server
app.listen(PORT, function () {
  console.log('Server up and running on ', `http://localhost:${PORT}/`);
});

搞好服务器,后端小伙伴 cors 跨域也不会弄,只能自己代理了

代理工具 http-proxy-middleware

const path = require('path');
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const PORT = 3001;

const STATIC = path.resolve(__dirname, 'dist');
const INDEX = path.resolve(STATIC, 'index.html');

const app = express();
// Static content
app.use(express.static(STATIC));
+ app.use(
+ '/userCode', // 请求 localhost:3001/userCode/login ->  
+ createProxyMiddleware({
+    target: 'http://10.1.81.45:9080/pf2/', //'http://10.1.81.45:9080/pf2//userCode/login
+   changeOrigin: true,
+  }),
+);


// All GET request handled by INDEX file
app.get('*', function (req, res) {
  res.sendFile(INDEX);
});

// Start server
app.listen(PORT, function () {
  console.log('Server up and running on ', `http://localhost:${PORT}/`);
});

相关文章

  • 使用nodejs做反向代理服务器

    index 使用nodejs做反向代理服务器 __veblen 为什么要反向代理?Nginx/Apache反向代理...

  • <HTTP权威指南>读书笔记 ---- Web代

    Web代理 Web的中间实体 Web代理(Web proxy)服务器是网络的中间实体。代理位于客户端和服务器之间,...

  • 如何通过网络代理服务器下载谷歌卫星地图

    如何通过网络代理服务器下载谷歌卫星地图 一、网络代理设置功能的作用 代理服务器处在客户机和服务器之间,对于远程服务...

  • vue+koa迁移到服务器流程

    koa首先需要调整端口号,不能和服务器上别的服务同端口 云服务器上开放端口,安全组开放 vue上做代理,然后npm...

  • Fiddler工具使用介绍三

    我们知道Fiddler是位于客户端和服务器之间的代理,它能够记录客户端和服务器之间的所有 HTTP请求,可以针对特...

  • Windows抓包工具--Fiddler配置及使用(安卓手机抓包

    一、什么是Fiddler? Fiddler是位于客户端和服务器端之间的代理,能够记录客户端和服务器之间的所有请求,...

  • fiddler使用

    一、Fiddler原理Fiddler是位于客户端和服务器之间的HTTP代理,负责监听客户端发起的请求和服务器端返回...

  • HTTP复习(四)——代理

    Web代理(proxy)服务器是网络的中间实体。代理位于客户端和服务器之间,扮演“中间人”的角色,在各端点之间来...

  • 第六章:代理

    Web代理(proxy)服务器是网络的中间实体。代理位于客户端和服务器之间,扮演“中间人”的角色,在各端点之间来回...

  • 2019-11-22

    Nodejs支持全部的服务器代码。包括web服务器和服务器端的脚本和任何支持web应用程序的功能。web服务器和任...

网友评论

      本文标题:2023-12-12- 🌦🌦nodejs做服务器和服务代理

      本文链接:https://www.haomeiwen.com/subject/endmgdtx.html