简体   繁体   English

在 Node.js 中包含 CSS 和 JS 文件

[英]Include CSS and JS files in Node.js

I am learning how to use Node.js but I'm stuck.我正在学习如何使用 Node.js,但我被卡住了。 I am unable to load css and js files in my project.我无法在我的项目中加载 css 和 js 文件。 I am loading Bootstrap and Fontawesome from a CDN and they're rendering fine.我正在从 CDN 加载 Bootstrap 和 Fontawesome,它们渲染得很好。 As for my own custom css and js, they are not loading at all.至于我自己自定义的 css 和 js,它们根本没有加载。

My folder file path:我的文件夹文件路径:

folder文件夹
index.html索引.html
app.js应用程序.js
package.json包.json
css css
main.css主文件
files档案
file.pdf文件.pdf

app.js:应用程序.js:

 var http = require('http');
     var fs = require('fs');

 //creating server
 http.createServer(function (req, res) {
   fs.readFile('index.html', function (err, html) {
     res.writeHead(200, { 'Content-Type': 'text/html' });
     res.write(html);
     res.end();

   });
 }).listen(8080);

I would suggest you create a public directory where you should keep all your javascript, CSS, images etc.我建议您创建一个公共目录,您应该在其中保留所有 javascript、CSS、图像等。

Now in app.js file, you can add this code to make all these files available anywhere in your node.js project.现在在 app.js 文件中,您可以添加此代码以使所有这些文件在您的 node.js 项目中的任何位置可用。

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

Don't forget to include path and express in your header like this:不要忘记在标题中包含路径和表达,如下所示:

var express = require('express');
var path = require('path');

Now you can use your CSS/JS files wherever you want like this.现在您可以像这样在任何地方使用您的 CSS/JS 文件。

<link rel='stylesheet' href='/style.css' />

Here style.css is your custom CSS file.这里 style.css 是您的自定义 CSS 文件。 Hope this way works fine to you.希望这种方式对你有用。

HTH Thanks!谢谢!

if your assets directory(css,js,fonts directory) on the root like below如果您的资产目录(css,js,fonts 目录)在根目录下,如下所示

在此处输入图片说明

then use this code然后使用此代码

in app.jsapp.js 中

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

and in .html file import css like that并在.html文件中像这样导入 css

<link rel="stylesheet" href="css/style.css">

if your assets are under another any parent folder like that如果您的资产在另一个这样的父文件夹下

public/assets/css/style.css

then just replace / with public/assets folder like below然后只需将/替换为public/assets文件夹,如下所示

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

and the <link> tag will remain same并且<link>标签将保持不变

There are various packages available to ease handling and creating the server.有多种软件包可用于简化处理和创建服务器。 Like Express , Connect etc.ExpressConnect等。

But if you prefer plain node.js I would suggest looking at below links.但是,如果您更喜欢普通的 node.js,我建议您查看以下链接。

https://gist.github.com/ryanflorence/701407 https://gist.github.com/ryanflorence/701407

http://www.tutorialsteacher.com/nodejs/serving-static-files-in-nodejs http://www.tutorialsteacher.com/nodejs/serving-static-files-in-nodejs

To summarize :总结一下:

var express = require('express');
var app = express();

//setting middleware
app.use(express.static(__dirname + 'public')); //Serves resources from public folder

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

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