由于今天上午 后端人员把接口都整合都一个服务器了,所以就没有硬关注 上一篇文章的问题,

晚上回来,用node搭了一个简单服务器,测试了下,是没有问题的。代码如下:

一、 自己初始化项目,

1、package.json

{
"name": "es",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"start": "webpack-dev-server --open --config webpack.dev.js",
"build": "webpack --config webpack.prod.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^3.2.0",
"csv-loader": "^3.0.2",
"express": "^4.17.1",
"file-loader": "^5.0.2",
"html-webpack-plugin": "^3.2.0",
"style-loader": "^1.0.1",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"webpack-dev-middleware": "^3.7.2",
"webpack-dev-server": "^3.9.0",
"webpack-merge": "^4.2.2",
"xml-loader": "^1.2.1"
},
"dependencies": {
"lodash": "^4.17.15"
}
}

2、webpack.common.js

const path = require('path');
// const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
app: './src/index.js'
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist')
},
devServer: {
proxy: {
'/yyy': {
target: "http://localhost:3000",
changeOrigin: true,
pathRewrite: {
'^/yyy': '/'
}
},
'/wpp': {
target: "http://localhost:4000",
changeOrigin: true,
pathRewrite: {
'^/wpp': '/'
}
},
}
},
plugins: [
// new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'Production'
})
],
};

3、webpack.dev.js

const merge = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
devtool: 'inline-source-map',
devServer: {
contentBase: './dist'
}
});

4、index.html

<!doctype html>
<html>
<head>
<title>起步</title>
<script src="https://unpkg.com/lodash@4.16.6"></script>
</head>
<body>
<script src="./src/index.js"></script>
</body>
</html>

5、src/index.js

function axios(api){
let xhr = new XMLHttpRequest();
xhr.open("get", api, true);
xhr.onload = () => {
console.log( xhr.response )
}
xhr.send()
} axios('/wpp/api/user');
axios('/yyy/api/name');

二、启动node服务,启动接口

server.js

let express = require('express')
let app = express() app.get('/api/name', (req, res) => {
res.json(
{ name: "yyy user" }
)
}) app.listen(3000)

server1.js

let express = require('express')
let app = express() app.get('/api/user', (req, res) => {
res.json(
{ name: "wpp user" }
)
}) app.listen(4000)

三、启动服务

1、  node server.js

2、  node server1.js      或可以在webpack中配置

3、  npm run start         ( 启动项目 )

之后,控制台中看到

{"name":"wpp user"}
{"name":"yyy user"}

以上为不同接口在不同服务器时,当合并到一个服务器时

如 server1.js 合并到  server.js

let express = require('express')
let app = express() app.get('/api/name', (req, res) => {
res.json(
{ name: "yyy user" }
)
})
app.get('/api/user', (req, res) => {
res.json(
{ name: "wpp user" }
)
}) app.listen(3000)

webpack.common.js 可以改成这种配置

devServer: {
proxy: [{
context: ['/yyy', '/wpp'],
target: "http://localhost:3000",
pathRewrite: {
'^/yyy': '/',
'^/wpp': '/'
}
}]
// proxy: {
// '/yyy': {
// target: "http://localhost:3000",
// changeOrigin: true,
// pathRewrite: {
// '^/yyy': '/'
// }
// },
// '/wpp': {
// target: "http://localhost:4000",
// changeOrigin: true,
// pathRewrite: {
// '^/wpp': '/'
// }
// },
// }
},

1、  node server.js

2、  npm run start         ( 启动项目 )

之后,控制台中看到

{"name":"wpp user"}
{"name":"yyy user"}

vue项目开发期间,配置webpack解决后台接口在不同服务器上的问题 之 二 ( node搭建服务 )的更多相关文章

  1. vue项目开发,用webpack配置解决跨域问题

    今天在本地开发时候碰到了跨域的问题,突然觉着跨域问题在所难免啊,之前没有没有碰到总觉着解决跨域很高大上的样纸,其实就是受限于网络的同源策略,跨域前后端都可以进行处理. 1,后端更改header hea ...

  2. vue项目构建:vue-cli+webpack常用配置

    1,Webpack-dev-server的proxy用法:https://www.jianshu.com/p/f489e7764cb8 2,vue-cli3搭建项目之webpack配置:https:/ ...

  3. 前端 vue-cli+Webpack 项目开发环境配置、创建一个vue-demo

    一.软件及命令: (1)下载node.js 最新的LTS 版本,下载 msi格式的(直接点击安装即可). (2)命令1:npm install cnpm -g 命令2:cnpm install web ...

  4. Vue项目开发最新、最全代码规范文档

    Vue项目开发最新.最全代码规范文档 2019年02月21日 10:43:49 yw00yw 阅读数 337   一. 目录结构 |— build 构建脚本目录 |— build.js 生产环境构建( ...

  5. Vue项目开发流程(自用)

    一.配置开发环境 1.1 安装Node.js npm集成在Node中,检查是否安装完成:node -v 1.2 安装cnpm(淘宝镜像) npm install -g cnpm,检查安装是否完成:cn ...

  6. Vue项目开发相关问题总结

    Vue项目开发相关问题总结 一.创建一个项目(两种方式) 1.通过CLI命令行创建,具体步骤如下: (1)Node 版本要求 Vue CLI 需要 Node.js 8.9 或更高版本 (推荐 8.11 ...

  7. Vue 项目开发

    目录 Vue 项目开发 项目目录结构解析 入口文件 main.js (项目入口) 根组件 app.vue index.html 文件入口 router 路由 components 子组件 项目初始化 ...

  8. VUE项目中使用this.$forceUpdate();解决页面v-for中修改item属性值后页面v-if不改变的问题

    VUE项目中使用this.$forceUpdate();解决页面v-for中修改item属性值后页面v-if不改变的问题:https://blog.csdn.net/jerrica/article/d ...

  9. vue从mock数据过渡到使用后台接口

    说明: 最近在搭建一个前端使用vue-element-admin,后端使用springBoot的项目. 由于vue-element-admin使用的是mock的模拟数据跑起来的项目,所以在开发过程中难 ...

随机推荐

  1. QT攻略——我在QT中遇到的那些坑

    (1)QUdpSocket接收数据 进入槽后,要用这种方式读取,否则可能会导致不发readyRead()信号 .while(udpSocket->bytesAvailable()){ udpSo ...

  2. Python格式化输出——format用法示例

    format OR % 提到Python中的格式化输出方法,一般来说有以下两种方式: print('hello %s' % 'world') # hello world print('hello {} ...

  3. EF Core 批处理语句

    在Entity Framework Core (EF Core)有许多新的功能,最令人期待的功能之一就是批处理语句.那么批处理语句是什么呢?批处理语句意味着它不会为每个插入/更新/删除语句发送单独的请 ...

  4. JavaWeb学习路线图(2020年最新版)

    Java基础 做java开发,java基础是最需要下功夫的一项.在校招时最注重的就是基础,拿不出像样的项目没关系,但是基础万万不可不牢固. 想要基础扎实,看书沉淀是必须的,有一些编程基础的同学推荐阅读 ...

  5. python第五章程序练习题

    5.2 def isOdd(a): if a%2!=0: return True else: a=eval(input()) print(isOdd(a)) 5.3 def isNum(x): try ...

  6. java异常的嵌套和级联

    一.分开捕获或者嵌套使用 我们先看看下面这段代码: public class Cal { public int div(int a, int b) { int result = a / b; retu ...

  7. Django model distinct 的使用方法

    原文: 今天突然有人问起在 django 的 model 里面怎么用 distinct, 对于这种东西,我一向的观点是查看django 的在线文档.于是不加思索的根据在线文档给出了答案,但结果很让人沮 ...

  8. 《CI/CD 流程以及原理说明》

    自动化部署 CI/CD 是一种通过在应用开发阶段引入自动化来频繁向客户交付应用的方法.CI/CD 的核心概念是持续集成.持续交付和持续部署.作为一个面向开发和运营团队的解决方案,CI/CD 主要针对在 ...

  9. Vue3.0报错error: Unexpected console statement (no-console) 解决办法

    写项目过程中用ESLint遵守代码规范很有必要,但是对于一些规范也很是无语,比如:‘Unexpected console statement (no-console)’,连console都不能用,这就 ...

  10. 文件转base64处理或转换blob对象链接

    一.文件转base64,代码: axios({ method: 'get', url: apiPath.common.downloaddUrl, responseType: 'blob'}).then ...