【发布时间】:2017-04-03 23:38:06
【问题描述】:
NodeJS https 服务器运行 api、cms 和 angular 应用程序。 当我使用 nodemon 在本地机器(macOS Sierra)上启动服务器时,一切正常。当我尝试在我的 VPS 上做同样的事情时,一切都失败了。 关于 VPS 的一些细节:它使用 NGINX 作为代理服务器,使用 pm2 作为集群管理器,并为 ssl 加密。
即使在防火墙中允许节点服务器的直接端口绕过 NGINX,角度前端和 CMS 也无法正确加载。从 pm2 切换到 nodemon 没有任何改变。
使用控制台,我从公共 node_modules 中获得每个依赖项所需的错误消息:
Refused to execute script from 'https://mydomain/node_modules/jquery/dist/jquery.min.js' because
its MIME type ('text/html') is not executable, and strict MIME typechecking is enabled.
当我们看到实际返回的内容时,它的 index.html 来自 angular 而不是请求的包。 出于测试目的,我允许以下操作
app.use('/node_modules', express.static(__dirname + '/node_modules'));
//With a copy of jquery and bootstrap
CMS 加载正确,但 Angular 前端没有。但它也暴露了服务器依赖项。你能帮我找出我的错误吗?
API 是一个简单的 REST API,使用一些服务与 MongoDB 进行通信。 CMS 由 Node 使用 ejs 视图引擎提供服务
//APP.JS
let cms = require('./routes/cms'); //An Express Router
let api = require('./routes/api');//An Express Router
//The modules containing angular, jquery and bootstrap for the Angular front end and the cms
//We separated the dependencies for the backend and frontend as a security measure
app.use('/node_modules', express.static(__dirname + '/public/node_modules'));
app.use('/api', api); //Server the api
app.use('/cms', cms); //Serve the cms
//The problem is probably here
app.use('/', express.static(__dirname + '/public')); //Route to the angular frontend
app.get('/*', function(req, res){ //Support for html5Mode in Angular
res.sendFile(path.join(__dirname + '/public/index.html'));
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
let err = new Error('Not Found\t' + req.header('Referer'));
err.status = 404;
next(err);
});
//Server.JS
let options = {
key: fs.readFileSync('certs/privatekey.pem'),
cert: fs.readFileSync('certs/certificate.pem')
};
let server = https.createServer(options,app);
//Angular Frontend index.html
<head>
<!— … —>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="node_modules/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="node_modules/animate.css/animate.min.css">
<link rel="stylesheet" href="node_modules/ng-dialog/css/ngDialog.min.css">
<link rel="stylesheet" href="node_modules/ng-dialog/css/ngDialog-theme-default.min.css">
<link rel="stylesheet" href="node_modules/angular-moment-picker/dist/angular-moment-picker.css">
<link rel="stylesheet" href="node_modules/angular-tooltips/dist/angular-tooltips.min.css">
<link rel="stylesheet" href="dist/css/site.css">
<!— … —>
</head>
//The tree
├── api.md //Some doc
├── app.js
├── bin //Contains server.js
├── certs
├── config
├── coverage
├── data
├── database //Link with the database
├── errors
├── helpers
├── logs
├── minigames
├── node_modules //The dependencies for the server
├── package.json //The package for the server
├── public
│ ├── app //The controllers for the Angular app
│ ├── app.js
│ ├── assets
│ ├── dist
│ ├── img
│ ├── index.html //Entry point for the Angular app
│ ├── karma.config.js
│ ├── node_modules //The node_modules for the Angular app
│ ├── npm-debug.log
│ ├── package.json //The package for the Angular app
│ ├── scss
│ ├── stylesheets
│ ├── superstatic.json
│ ├── tests
│ └── views
├── routes //Contains the routes for the Api and the CMS
├── services
├── test
├── tools
└── views //The views for the CMS
【问题讨论】:
标签: javascript angularjs node.js express nginx