簡體   English   中英

平均值:服務於Angular node_modules的NodeJS返回index.html

[英]MEAN: NodeJS serving Angular node_modules returns index.html

運行api,cms和angular應用程序的NodeJS https服務器。 當我使用nodemon在本地計算機(macOS Sierra)上啟動服務器時,一切正常。 當我嘗試在VPS上執行相同操作時,一切都會失敗。 有關VPS的一些詳細信息:它使用NGINX作為代理服務器,使用pm2作為集群管理器,並使用letencrypt for 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.

當我們看到實際返回的內容是來自angular的index.html而不是所請求的包。 出於測試目的,我允許以下操作

 app.use('/node_modules', express.static(__dirname + '/node_modules'));
//With a copy of jquery and bootstrap

並且CMS正確加載,但前端沒有傾斜。 但這也暴露了服務器依賴性。 您能幫我發現我的錯誤嗎?

api是使用某些服務與MongoDB通信的簡單REST API。 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

似乎public / node_modules為空。 npm install似乎可以正常工作,因為它沒有引發錯誤,但是在某處失敗。 通過sftp復制node_modules解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM