vue project needs to be published by node
and nginx
, but there are some problems that are not very clear. I"d like to ask you:
requirements:
currently there is only one front-end server, so node
and nginx
are all installed together, and the back-end api
is published according to business, so I need to configure multiple api
server addresses.
known conditions
- Domain name
xxxx.com
- ip
10.12.11.xx:80
directory structure:
-
xxxUI-- Root path
- package.json-- Server index.js dependency configuration
- index.js-- web server code created based on node
- node-modules-- the package folder that index.js depends on
- www-- the root folder of the front-end project
node create server code index.js
code is as follows:
var express = require("express");
var httpProxy = require("http-proxy");
var app = express();
var proxy = httpProxy.createProxyServer({
target: "10.12.11.xx:80/api",
changeOrigin: true
});
//web
app.use(express.static("www"));
app.use("/api", (req, res) => {
proxy.web(req, res);
});
app.use("/", function(req, res) {
res.sendFile(__dirname+"/www/index.html");
});
proxy.on("error", function (err, req, res) {
console.log("proxy error", err);
res.writeHead(500, {"Content-Type" : "text/plain"});
res.end("err");
});
var server = app.listen(9988, (req, res) => {
});
question:
- assuming that I do not need to use nginx, then the node server can listen on the
80
port, so it can be accessed normally when accessingxxxx.com
, but if I need to usenginx
, thennode (the server corresponding to the front-end project-- created through
httpProxy.createProxyServer)
must not useport 80
, and the80 port
must be reserved fornginx.
- when I create a front-end project server (
index.js file
) through node api, I can only create one api proxy address. How do I configure it? Do you want to modify the node api code for creating the server or modify the configuration ofnginx
(because the corresponding api paths of different modules are different, for example: member modulexxx.com/api/member/xxx
, registration login modulexxx.com/api/system/xxx
)? How to configure it?