我搜索了整个互联网,无法找到答案。 我刚刚开始使用自己的nginx服务器并正在部署一个nodejs项目,我使用的是angular 4和expressjs。当我使用位置' /'在nginx中,但当我改为' / dbproject'它确实有效。
首先,它确实在我的公共文件夹中找到了.js文件但是当我使用ng build --deploy url = / dbproject建立了角度时,它找到了它,但后来我得到了错误:
Uncaught SyntaxError: Unexpected token <
polyfills.bundle.js:1 Uncaught SyntaxError: Unexpected token <
styles.bundle.js:1 Uncaught SyntaxError: Unexpected token <
vendor.bundle.js:1 Uncaught SyntaxError: Unexpected token <
main.bundle.js:1 Uncaught SyntaxError: Unexpected token < .
奇怪的是,当我使用位置&#39; /&#39;在nginx。
这是nginx设置:
location /dbproject {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
这是app.js文件:
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const db = require('./config/db');
const app = express();
const users = require('./routes/users');
db.connect(db.MODE_PRODUCTION, function(err) {
if (err) {
console.log('Unable to connect to MySQL.')
process.exit(1)
} else {
console.log("Connectad");
}
});
// Port Number
const port = 3000;
// CORS Middleware
app.use(cors());
// Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));
// Body Parser Middleware
app.use(bodyParser.json());
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
app.use('/users', users);
// Index Route
app.get('/', (req, res) => {
res.send('Invalid Endpoint');
});
app.get('*', (req, res) =>{
res.sendFile(path.join(__dirname, 'public/index.html'));
});
// Start Server
app.listen(port, () => {
console.log('Server started on port '+port);
});
答案 0 :(得分:0)
它已经解决了。
在:
location /dbproject {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
后:
location /dbproject {
proxy_pass http://localhost:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}