简体   繁体   中英

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

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'));
});

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..

you have defined the public dir in app root/public

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

so you have to use:

./css/main.css

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 ()"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM