Failed to load resource: the server responded with a status of 404 (Not Found) css

為{幸葍}努か 提交于 2021-02-06 13:54:55

问题


I'm having trouble getting my browser to display the css for the app I am creating. I have looked at the same question asked by other users but have not found any of the answers to help in my situation. When I goto the page, all that is displayed is "Hello world" with no styling even though the stylesheet is linked. When I inspect the page, I get the error:

Failed to load resource: the server responded with a status of 404 (Not Found)

here's the structure of my application

.
./public 
     /css
       /main.css
./routes
     /index.js
./views
     /index.html
./server.js

server.js:

const express = require('express');
const app = express();
const port = process.env.PORT || 8080;

app.use(express.static(__dirname + '/public')); 
const router = require('./routes/index.js');
app.use('/',router);

app.listen(port);

console.log('connected to port: ' + port);

index.html

<!DOCTYPE html>
<html>
<head>
<link href="../public/css/main.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

</head>
 <body>
    <p>Hello World!</p>
 </body>

index.js

var express = require('express');
var path = require('path');
var router = express.Router();
module.exports = router;

router.get('/', function(req,res){
res.sendFile(path.join(__dirname,'../views/index.html'));
});

回答1:


you have defined the public dir in app root/public

app.use(express.static(__dirname + '/public')); 

so you have to use:

./css/main.css



回答2:


Use the following Code:-

../css/main.css

Note: The "../" is shorthand for "The containing directory", or "Up one directory".

If you don't know the previous folder this will be very helpful..




回答3:


i use firebase-database in html signup but last error i cannot understand if anybody know tell me . error is "Failed to load resource: the server responded with a status of 404 ()"



来源:https://stackoverflow.com/questions/47517703/failed-to-load-resource-the-server-responded-with-a-status-of-404-not-found-c

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