admin管理员组

文章数量:1426631

I am using express and node.js in backend and ejs templating engine in front-end. My app.js look like this

app.get('/book/:id', (req, res)=>{
var book_id = req.params.id;
console.log('this book :', book_id);
Book.findOne({
    book_id
}).then((book)=>{
    res.render('book.ejs', {book, title: "Book"});
}).catch((e)=>{
    // console.log(e);
});
});

book.ejs file

<!DOCTYPE html>
<html>
<head>
    <title><%= title %></title>
    <script type="text/javascript" src="jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="books.js"></script>
</head>
<body style="font-family: arial">
    <h1><%= title %></h1>
</body>
</html>

But when i route page to book/1234, I got following log in my server

this book : 1234
this book : jquery-3.3.1.min.js
this book : book.js

Why jquery-3.3.1.min.js and book.js are send to book/:id route? I am only sending book/1234 but jquery-3.3.1.min.js and book.js are also sent to server and causing error.

Browser console log is this

GET http://localhost:3000/book/jquery-3.3.1.min.js net::ERR_ABORTED 500 (Internal Server Error)
1234:1 Refused to execute script from 'http://localhost:3000/book/jquery-3.3.1.min.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
1234:6 GET http://localhost:3000/book/books.js net::ERR_ABORTED 500 (Internal Server Error)
1234:1 Refused to execute script from 'http://localhost:3000/book/books.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

I am using express and node.js in backend and ejs templating engine in front-end. My app.js look like this

app.get('/book/:id', (req, res)=>{
var book_id = req.params.id;
console.log('this book :', book_id);
Book.findOne({
    book_id
}).then((book)=>{
    res.render('book.ejs', {book, title: "Book"});
}).catch((e)=>{
    // console.log(e);
});
});

book.ejs file

<!DOCTYPE html>
<html>
<head>
    <title><%= title %></title>
    <script type="text/javascript" src="jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="books.js"></script>
</head>
<body style="font-family: arial">
    <h1><%= title %></h1>
</body>
</html>

But when i route page to book/1234, I got following log in my server

this book : 1234
this book : jquery-3.3.1.min.js
this book : book.js

Why jquery-3.3.1.min.js and book.js are send to book/:id route? I am only sending book/1234 but jquery-3.3.1.min.js and book.js are also sent to server and causing error.

Browser console log is this

GET http://localhost:3000/book/jquery-3.3.1.min.js net::ERR_ABORTED 500 (Internal Server Error)
1234:1 Refused to execute script from 'http://localhost:3000/book/jquery-3.3.1.min.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
1234:6 GET http://localhost:3000/book/books.js net::ERR_ABORTED 500 (Internal Server Error)
1234:1 Refused to execute script from 'http://localhost:3000/book/books.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
Share Improve this question edited Sep 30, 2018 at 7:19 Estus Flask 224k79 gold badges472 silver badges612 bronze badges asked Sep 30, 2018 at 7:03 Sanket SanathSanket Sanath 431 silver badge7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Since script links have relative paths, they are loaded from current path, which is /book.

They should either have absolute paths:

<script type="text/javascript" src="/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/books.js"></script>

Or base URL should be specified:

<base href="/">

I think that your book.ejs file is under the route of '/book'. if that so, you script tag like <script type="text/javascript" src="books.js"></script> will access the route of /book/book.js ,not your assets. so you should set you src attribute like this /book.js and make sure you had made your assets accessible.

You are using relative path in src attribute. Since you are serving the page from /books/<id>, if you use a relative path, browser will understand it as /books/(relative_path_of_resource) and so when it es across those links it is sending a request to books/jquery.js and books/books.js

You should convert your link to point to the correct static path of the js files. Refer this link - https://expressjs./en/starter/static-files.html to see how to serve static files and once you set up the serving, you can change the links to /static/books.js and /static/jquery.js

本文标签: When I link javascript file in html it sends a request to server and causing errorStack Overflow