如何将文件布置到服务器
完整代码
/*
* 本地部署dist 项目文件
*/
const path = require("path");
//导入express
const express = require('express')
//创建web服务器
const app = express()
const {
createProxyMiddleware
} = require("http-proxy-middleware");
// 代理对象地址
app.use(
"/manage",
createProxyMiddleware({
// target: "http://10.8.1.20:8002",
target: 'http://10.8.1.50:8002',
changeOrigin: true,
// pathRewrite: {
// '^api': ''
// }
})
);
app.use("/digitalFurniture", express.static(path.join(__dirname, "./dist")));
// 访问http://localhost:8033/digitalFurniture
// 通过ap.listen进行服务器的配置,并启动服务器,接收两个配置参数,一个是对应的端口号,一个是启动成功的回调函数
app.listen(8033, () => {
console.log('服务器启动成功');
})
一、新建文件夹
通过npm init初始化配置文件
一直回车,默认就行了
(NODEJS)文件夹中添加app.js文件 & 需要部署的dist文件夹
NODEJS
|- package.json
|- package-lock.json
+ |- app.js
+ |- /dist
|- /static
|- favicon.ico
|- index.html
二、通过npm安装所需要的第三方依赖包
$ npm add express
$ npm add nodemon
$ npm install --save-dev http-proxy-middleware
三、运行
$ node app.js
访问(浏览器打开)
http://localhost:8033/digitalFurniture
注:
/digitalFurniture 项目前缀(根据项目而定)
部署项目前缀 app.use(“/digitalFurniture”, express.static(path.join(__dirname, “./dist”)));