mongodb 是否启用了拒绝执行脚本MIME类型检查?

irlmq6kh  于 2023-03-29  发布在  Go
关注(0)|答案(1)|浏览(110)

我一直在控制台上得到这个错误。:

  • “拒绝执行来自'http://localhost:3000/js/main.js'的脚本,因为其MIME类型('text/html')不可执行,并且启用了严格的MIME类型检查。"*

我尝试了每一种方法,我可以找到在互联网上,他们没有一个工作。我分享我的代码片段。我想我的注册形式,以指导我的主页,但它不指导我的任何地方。它只是给我的控制台上的错误。我分享了我的index.js adminRoutes.js和indexRoutes.js代码与您。我用mongodb nodejs和express.js在这个项目上。我只想点击注册,我希望它能引导我到主页,这是定义为/

如果需要,我可以添加更多代码。

indexRoutes.js:

const express = require('express'),
router = express.Router();


//ARRAY

let data= [
    {
        postTitle: "Blog Denemesi",
        postSubtitle: "Bu ilk blog denemesi ve video çok sıkıcı.",
        image: "https://images.unsplash.com/photo-1564177611049-76e2933c6017?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=464&q=80"

    },

    {
        postTitle: "Testing a blog",
        postSubtitle: "This is a blog testing",
        image: "https://images.unsplash.com/photo-1552664730-d307ca884978?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80"

    },

    {
        postTitle: "Test Test",
        postSubtitle: "Lets see whats gonna happen",
        image: "https://images.unsplash.com/photo-1562762394-3acfba62a48e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80"

    },



]

//ARRAY Bitiş

router.get("/", (req, res)=>{
    res.render('home' , {data : data});
 }) ;

 router.get("/about", (req,res)=>{
    res.render('about');
 }) ;

 router.get("/contact", (req,res)=>{
    res.render('contact');
 }) ;


module.exports = router;

adminRoutes.js:

const express = require('express'),
passport = require("passport"),
User= require('../models/userModel'),
router = express.Router();

//ARRAY

let adminActions =[

    {
        actionId:1,
        actionName: "changeHomeImage",
        displayName: "Change Home Image"

    },
    {
        actionId:2,
        actionName: "changeAboutImage",
        displayName: "Change About Image"

    },
    {
        actionId:3,
        actionName: "changeAboutText",
        displayName: "Change About Text"

    },
    {
        actionId:4,
        actionName: "addNewBlog",
        displayName: "Add New Blog"

    },
    {
        actionId:5,
        actionName: "listAllBlogs",
        displayName: "List All Blogs"

    }
];

//ARRAY Bitiş

router.get("/admin", (req, res)=>{
    res.render("admin/admin",{adminActions:adminActions});
 }) ;

 router.get("/signin", (req,res)=>{
    res.render("admin/signin");
 }) ;

 router.post("/signin", (req,res)=>{
 }) ;

 router.get("/signup", (req,res)=>{
    res.render('admin/signup');
 }) ;

 router.post("/signup", (req,res)=>{
   
    let newUser = new User({username:req.body.username});
    User.register(newUser, req.body.password, (err, user)=>{
        if (err){
            console.log(err);
            res.redirect("/signup");
        }
        passport.authenticate("local")(req, res, ()=>{
            res.redirect("/");

        });
    })
}) ;

module.exports = router;

index.js:

const mongoose = require("mongoose"),
    express= require ("express"),
    passport = require("passport"),
    LocalStrategy = require("passport-local"),
    User= require("./models/userModel"),
    expressSession = require("express-session"),
    bodyParser = require("body-parser"),

    app = express();

const { Passport } = require("passport");
    //Routes

    const indexRoutes = require("./routes/indexRoutes");
          adminRoutes = require("./routes/adminRoutes");


    //App Config

mongoose.connect("mongodb://localhost/BlogApp");
app.set('view engine' , 'ejs');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended:true}));

//Passport Config

app.use(require("express-session")({
     secret: "bu güvenlik cümlesi",
     resave: false,
     saveUninitialized: false

}));

app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

//Routes Using

app.use(indexRoutes);
app.use(adminRoutes);

//Server 

const server = app.listen(3000, (err)=> {
    if(err){
      console.log(err);
    }

      console.log('App Started. Port number  : %d ', server.address().port);

})

signup.ejs:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <meta name="description" content="" />
        <meta name="author" content="" />
        <title>Clean Blog - Start Bootstrap Theme</title>
        <link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
        <!-- Font Awesome icons (free version)-->
        <script src="https://use.fontawesome.com/releases/v6.3.0/js/all.js" crossorigin="anonymous"></script>
        <!-- Google fonts-->

        <!-- Core theme CSS (includes Bootstrap)-->
        <link href="css/main.css" rel="stylesheet" />
        <!--OLDU MU EMİN DEĞİLİM-->

        <!--Bootstrap Link-->
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">

        <!--Bootstrap FONTAWESOME-->
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.2.1/css/fontawesome.min.css" integrity="sha384-QYIZto+st3yW+o8+5OHfT6S482Zsvz2WfOzpFSXMF9zqeLcFV0/wlZpMtyFcZALm" crossorigin="anonymous">

        <!--JQuery CDN-->
        <script
           src="https://code.jquery.com/jquery-3.6.4.js"
           integrity="sha256-a9jBBRygX1Bh5lt8GZjXDzyOB+bWve9EiO7tROUtj/E="
           crossorigin="anonymous"></script>

           <style>
            #mainNav .navbar-brand{
                color: black;
            }
            #mainNav .navbar-brand:hover{
                color: coral;



            }
            #mainNav .navbar-nav > li.nav-item > a{
                color: black;
                background-color: black;
                
                
            }
            #mainNav .navbar-nav > li.nav-item > a:hover{
                color: coral;

            }
           </style>

    </head>
    <body>
        <!-- Navigation-->
        <nav class="navbar navbar-expand-lg navbar-light" id="mainNav">
            <div class="container px-4 px-lg-5">
                <a class="navbar-brand" href="/">Start Bootstrap</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                    Menu
                    <i class="fas fa-bars"></i>
                </button>
                <div class="collapse navbar-collapse" id="navbarResponsive">
                    <ul class="navbar-nav ms-auto py-4 py-lg-0">
                        <li class="nav-item"><a class="nav-link px-lg-3 py-3 py-lg-4" href="/">Home</a></li>
                        <li class="nav-item"><a class="nav-link px-lg-3 py-3 py-lg-4" href="/about">About</a></li>
                        <li class="nav-item"><a class="nav-link px-lg-3 py-3 py-lg-4" href="/contact">Contact</a></li>
                        <li class="nav-item"><a class="nav-link px-lg-3 py-3 py-lg-4" href="/signin">Sign In</a></li>
                    </ul>
                </div>
            </div>
        </nav>

<div class="container" style="margin-top: 5%;">
    <h1 style="text-align: center;">Sign Up Form</h1>
    <h3 style="text-align: center; color: red;">ADMIN</h3>
<form>
    <div class="form-group">
      <label for="exampleInputEmail1">Email address</label>
      <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
      <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
    </div>
    <div class="form-group">
      <label for="exampleInputPassword1">Password</label>
      <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
    </div>
    <div class="form-check">
      <input type="checkbox" class="form-check-input" id="exampleCheck1">
      <label class="form-check-label" for="exampleCheck1">Check me out</label>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>

</div>

<%- include ('../partials/footer') %>

main.js:

/*!
* Start Bootstrap - Clean Blog v6.0.9 (https://startbootstrap.com/theme/clean-blog)
* Copyright 2013-2023 Start Bootstrap
* Licensed under MIT (https://github.com/StartBootstrap/startbootstrap-clean-blog/blob/master/LICENSE)
*/
window.addEventListener('DOMContentLoaded', () => {
    let scrollPos = 0;
    const mainNav = document.getElementById('mainNav');
    const headerHeight = mainNav.clientHeight;
    window.addEventListener('scroll', function() {
        const currentTop = document.body.getBoundingClientRect().top * -1;
        if ( currentTop < scrollPos) {
            // Scrolling Up
            if (currentTop > 0 && mainNav.classList.contains('is-fixed')) {
                mainNav.classList.add('is-visible');
            } else {
                console.log(123);
                mainNav.classList.remove('is-visible', 'is-fixed');
            }
        } else {
            // Scrolling Down
            mainNav.classList.remove(['is-visible']);
            if (currentTop > headerHeight && !mainNav.classList.contains('is-fixed')) {
                mainNav.classList.add('is-fixed');
            }
        }
        scrollPos = currentTop;
    });
})
mnowg1ta

mnowg1ta1#

我解决了这个问题,写下如何做,以防有人会面临同样的错误。
1)将js文件夹移动到公共文件夹中2)在signup.ejs中编写下面的代码:

<script>
document.getElementById("adminSignup").onsubmit = (e) => {
   console.log("dsadsa");
   e.preventDefault();
   window.location.href = "/";
};

3)给予表单一个ID:

<form id="adminSignup">

4)在index.js中添加

app.use(express.static('js'));

这就是全部!现在你的代码已经准备好工作了!

相关问题