[英]express.static files dont load static files
I am trying to server static files with express and them dont work, i think its express problem or something but i dont realize why it dont works.我正在尝试使用 express 服务器 static 文件,但它们不起作用,我认为它有 express 问题或其他问题,但我不明白为什么它不起作用。 my folders look like this:
我的文件夹如下所示:
app.js public --css --js --images app.js public --css --js --images
and the code i trying to run like this:和我试图像这样运行的代码:
app.use(express.static(path.join(__dirname,'public')));
if i do console.log of the path如果我执行路径的 console.log
console.log(path.join(__dirname, 'public'))
i get the next path C:\Users\J\Desktop\node.js\social-media-cars\public我得到下一条路径 C:\Users\J\Desktop\node.js\social-media-cars\public
and if i look at the path with the windows file explorer i see that the path is correct and i see the content inside, so i dont think its path fault如果我使用 windows 文件资源管理器查看路径,我会发现路径是正确的并且我会看到里面的内容,所以我不认为它的路径错误
i also tryed:我也试过:
app.use(express.static(path.join(__dirname,'public')));
app.use('/static',express.static(path.join(__dirname, '/public')));
and dont works, always the same things:并且不工作,总是一样的事情:
im desesparate with this, idk why this dont works.我对此很失望,我不知道为什么这不起作用。
code where i use it looks like this:我使用它的代码如下所示:
// Import the express module
const path = require('path');
const express = require("express");
// Instantiate an Express application
const app = express();
const dotenv = require("dotenv").config();
const ActionsRouter = require('./routers/actions-router');
const UserRouter = require('./routers/user-router');
const cookieparser = require('cookie-parser');
// app.use('/static',express.static(path.join(__dirname, '/public')));
//set static files that are rendered after
app.use(express.static(path.join(__dirname,'public')));
//read the cookies from response
app.use(cookieparser(path.join(__dirname, '/public')));
console.log(path.join(__dirname, 'public'))
//create the req.body
app.use(express.json());
//user
app.use("/api",UserRouter);
app.use("/useractions",ActionsRouter);
module.exports = app;
const server = app.listen(port,()=>{
console.log(`listening on port ${port}`)});
get the static files with every response i get得到 static 文件以及我得到的每个响应
index.js
or app.js
above you set view engine index.js
或者app.js
中添加这个中间件 app.use(express.static(path.join(__dirname, "public")));
|__public/
|__ css/
|__ css files...
|__ js/
|__ js files...
3. Import this way 3.这样导入
Now you set the path to the public directory you have to give the path public folder when you import现在您将路径设置为公共目录,您必须在导入时提供路径公共文件夹
<link rel="stylesheet" href="/css/main.css" />
In the same way, you can import your JS files以同样的方式,你可以导入你的JS文件
okey i found it, i was so lost because when i try to see the files that i got in each response on the browser, i was not getting any file, but only when i linked css and html it downloaded the static file, idk why this works like this, i supposed every of /public file was atached to the response you use it or not好吧,我找到了,我迷路了,因为当我试图在浏览器上查看我在每个响应中获得的文件时,我没有得到任何文件,但只有当我链接 css 和 html 时,它才会下载 static 文件,我知道为什么这是这样工作的,我想每个 /public 文件都附加到你是否使用它的响应
<link rel="stylesheet" type="text/css" href="/css/styles.css">
anyway if there is a way to get the file you request or not i would like to know无论如何,如果有办法获取您请求的文件,我想知道
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.