Using nginx to proxy static files from node js (ex

2019-08-06 18:37发布

I have a basic nginx setup, where requests to example.com/xyz1 are proxied to 127.0.0.1:9000 and requests to example.com/xyz2 are proxied to 127.0.0.1:9001.

On 127.0.0.1:9000 runs an express server, that should only server static files. For my index.html there is no problem. But all dependencies like css and JS files can not be resolved.

const express = require('express');
const bodyParser= require('body-parser');

const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use('/', express.static(__dirname + 'public'));

const MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/xyz1", (err, database) => {
    if (err) return console.log(err);
    db = database;

    console.log('Connected to mongoDb');
    app.listen(9000, () => {
            console.log('Express-Server listening on port 9000');
    })
})

app.get('/xyz1', (req, res) => {
    console.log('xyz1');
    res.sendFile(__dirname + '/public/' + 'index.html');
    //res.send('Hello World xyz1');
})

My folder structure for xyz1:

  • xyz1
    • server.js
    • ...
    • public
      • index.html
      • style
        • css files
      • scripts
        • JS files

if some needs to see the nginx configuration, please tell me.

0条回答
登录 后发表回答