繁体   English   中英

拒绝执行 javascript 因为 MIME 类型为空,nodeJS + express + engintron + nginx

[英]Refused to execute javascript because MIME type is empty, nodeJS + express + engintron + nginx

我目前有一个问题导致我把头发拉出来。

所以这是你需要知道的

我的应用程序正在运行 node.js (16.13.1),通过 engintron 使用 express 和 nginx,因为该应用程序由 cPanel 托管。

这是我的 app.mjs

//The dependencies the app needs
//------------------------------------------------------------------------------------------------------------------------
import express from 'express'; //Imports the express package that we installed with npm install express

import request from 'request'; //Imports the express package that we installed with npm install request

import bodyParser from 'body-parser'; //Imports the express package that we installed with npm install bodyParser

import path from 'path'; //Imports the express package that we installed with npm install path

//------------------------------------------------------------------------------

const expressapp = express(); //Creates the application with express

const moduleURL = new URL(import.meta.url);

const __dirname = path.dirname(moduleURL.pathname);

//Middleware

expressapp.use(express.json()); //Tells express to use JSON as it's input

expressapp.use(bodyParser.urlencoded({ extended: false })); //Encodes the URL in the body of the app's request so that they remain secure.

expressapp.use(express.static(__dirname, {index: 'index.html'})); //Tells the app to use the current path, with the index being index.html

console.log("The directory used is", (path.join(__dirname, '/assets')));

expressapp.post('/subscribe', (req, res) => { //Makes a post request to the subscribe route where req is the request and res is the response
    const { email, js } = req.body; //Create a constant that makes up of the request's body

    const mcData = { REDACTED } //This app involves sending a request to the MailChimp API, so this JSON object contains some of my authorization keys :)

    const mcDataPost = JSON.stringify(mcData); //Turns the JSON object into a string

    const options = { REDACTED } //Some more personal data here

    if (email) { REDACTED } //The code where the email that gets sent to the API, this has been confirmed to be working as intended. 
});


expressapp.listen(5000, console.log('Server started on port 5000!')); //Console log that confirms the start of the server

export default expressapp;

接下来是我在 index.html 中的声明。

<script type="application/javascript" src="../app.mjs"></script>

另外请注意,我在 etc/nginx/mime.types 文件中有 application/javascript 作为我的 javascript MIME 类型。

说到 nginx,这是我的 default.conf,如果这很重要(我不太确定)

server {
    #listen 80 default_server;
    listen 80;
    
    server_name domain name, followed by IP address; 

    # deny all; # DO NOT REMOVE OR CHANGE THIS LINE - Used when Engintron is disabled to block Nginx from becoming an open proxy

    # Set the port for HTTP proxying
    set $PROXY_TO_PORT 8080;

    include common_http.conf;
}

common_http.conf

location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    proxy_pass http://127.0.0.1:5000/;
    try_files $uri $uri/ @backend;
}

nginx.conf

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

当我的应用程序启动时,它拒绝执行 app.mjs 脚本,因为它的 MIME 类型是 ('')。 我想知道究竟是什么导致 MIME 类型不被传递,以便我的网站可以正常运行。 如果有人可以就这里发生的事情给我一些帮助,我将不胜感激。

错误证明: https://imgur.com/a/a3bVf13

您可以通过将模块 javascript 类型显式添加到 nginx/etc/mime.types 文件来修复它,如下所示: https://imgur.com/a/WytqyVW

使用某种文本编辑器(例如 nano)来执行此操作,只是想通了,哈哈。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM