When I link javascript file in html it sends a request to server and causing error

[亡魂溺海] 提交于 2019-11-28 12:02:57

问题


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.

回答1:


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="/">



回答2:


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.




回答3:


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 comes 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.com/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



来源:https://stackoverflow.com/questions/52575467/when-i-link-javascript-file-in-html-it-sends-a-request-to-server-and-causing-err

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!