简体   繁体   中英

Include .css and .js file in Node.js project

I used Node.js and Angular.js to build a simple web application based on Express framework, when I try to upload a .css file, I got the following error message :

“The stylesheet was not loaded because its MIME type, ”text/html“ is not ”text/css"

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var pg = require('pg');
var jwt = require('jsonwebtoken'); // create, sign and verify tokens
var morgan = require('morgan');
// Application front end
app.get('*',function(req,res){
  res.sendfile('./public/index.html')
})
app.listen(port);
console.log('LogAnalysisWebApp happens on port '+port);

<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <title>Log Analysis Web APP</title>
    <link rel="stylesheet" type="text/css" href="/public/stylesheets/style.css">
  </head>
  <body>
    <h1>Text Example</h1>
  </body>
</html>

My project has the following structure :

项目结构

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express.

app.use(express.static('public'));

Now, you can load the files that are in the public directory:

http://www.domain.com/stylesheets/style.css

Here is the official documentation page on serving static resources.

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