Jithesh Gopinathan
Jithesh Gopinathan

Reputation: 448

Getting 404 error for CSS and JS files in ExpressJS application

I am new to the client side programming, building a nodeJS express sample application, but getting 404 error for loading script and CSS.

Please find the below wolder structure: Folder structure

HTML page is as below:

<head>
<meta charset="UTF-8">
<title>Favorite Book</title>
<link rel="stylesheet" href="../public/css/main.css" type="text/css"/>
<script type="text/javascript" src="../public/libs/js/angular.min.js"></script>
<script type="text/javascript" src="../public/libs/js/angular-route.min.js"></script>
<script type="text/javascript" src="../public/libs/js/jquery-1.12.2.min.js"></script>
<script src="../public/libs/js/script.js"></script>

Script error getting from browser is below:

GET http://localhost:3000/public/libs/js/script.js

Upvotes: 0

Views: 135

Answers (1)

KibGzr
KibGzr

Reputation: 2093

you should set static path in app.js

app.use(express.static(path.join(__dirname, 'public')));

then defind source without public path like this

<link rel="stylesheet" href="/css/main.css" type="text/css"/>

Upvotes: 3

Related Questions