Some questions about the release of the front-end `vue` site?

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:

  1. 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 accessing xxxx.com , but if I need to use nginx , then node (the server corresponding to the front-end project-- created through httpProxy.createProxyServer ) must not use port 80 , and the 80 port must be reserved for nginx.
  2. 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 of nginx (because the corresponding api paths of different modules are different, for example: member module xxx.com/api/member/xxx , registration login module xxx.com/api/system/xxx )? How to configure it?
Apr.25,2021

  1. generally yes, unless you nginx doesn't listen on port 80, it doesn't matter (but who does).
  2. according to the description, you have multiple services to provide api . It is best not to put the proxy part in the node service, so the coupling is too high, through the nginx proxy. In my opinion, your node service is not needed at all and can be done on nginx .

nodejs is not designed to provide static services and proxies, of course, these node can be done, but nginx does better and more professional, why don't we use professional tools to do what he does best?

Menu