简体   繁体   English

在Node.js项目中包含.css和.js文件

[英]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 : 我使用Node.js和Angular.js构建基于Express框架的简单Web应用程序,当我尝试上传.css文件时,出现以下错误消息:

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

server.js server.js

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

index.html 的index.html

<!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. 若要提供静态文件,例如图像,CSS文件和JavaScript文件,请使用Express中的express.static内置中间件功能。

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. 这是有关提供静态资源的官方文档页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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