Express app定义的`static`文件夹无法正常工作(Express app defined `static` folder not working properly)

在我的express应用程序中,我已经声明了静态文件夹。 而且我还将我的index.html保存在静态文件夹中。 但我为新目的创建了一个新的router 。 当我创建一个新的router我的静态文件夹总是试图使用声明的新router获取资产。 任何人帮我解决这个问题?

这是我的代码和问题:

var 
express = require("express"),
app     = express(),
Router  = express.Router;


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

var oneTime = new Router(); //my new router


oneTime.get("*", function( req, res ) {

    res.sendFile( __dirname + "/public/oneTime/index.html");

});

app.use("/oneTime", oneTime);

app.listen( 9000, function portCallback() {

    console.log(" I am started!!" , __dirname);

})

但我的应用程序试图获得像这样的angular js:

HTTP://本地主机:9000 /一次性/一次性/ angular.min.js

问题是什么?

这是html我怎么称呼我的angualr:

<script src="oneTime/angular.min.js"></script>

实际上我期待这样:

HTTP://本地主机:9000 /大众/一次性/ angular.min.js


In my express app, I have declared the static folder. And I am keeping my index.html as well in the static folder. But I have created a new router for a new purpose. when i create a new router my static folder always trying to get the assets using the new router declared. any one help me to sort this issue?

here is my code and issue :

var 
express = require("express"),
app     = express(),
Router  = express.Router;


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

var oneTime = new Router(); //my new router


oneTime.get("*", function( req, res ) {

    res.sendFile( __dirname + "/public/oneTime/index.html");

});

app.use("/oneTime", oneTime);

app.listen( 9000, function portCallback() {

    console.log(" I am started!!" , __dirname);

})

But my app trying to get the angular js like this:

http://localhost:9000/oneTime/oneTime/angular.min.js

what is the issue?

here is the html how i call my angualr:

<script src="oneTime/angular.min.js"></script>

actually I am expecting like this:

http://localhost:9000/public/oneTime/angular.min.js

2023-03-27 21:03

满意答案

选项1

你可以改变这一行:

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

app.use('/oneTime', express.static(__dirname + '/puplic')); //my public folder

选项2

我假设你在公共文件夹中有一个js文件夹,然后你需要更改你的HTML代码:

<script src="js/angular.min.js"></script>

并且服务器代码将是

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

Option 1

You can change this line:

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

to

app.use('/oneTime', express.static(__dirname + '/puplic')); //my public folder

Option 2

I am assuming you have a js folder in the public folder, then you need to change your html code:

<script src="js/angular.min.js"></script>

and the server code will be

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

相关问答

更多

表示从隐藏(点)文件夹中提供静态文件(Does express serve static files from hidden (dot) folder)

在我无法提供隐藏文件后,我通过谷歌搜索发现了这个问题。 我发现express默认不提供它们。 您可以使用dotfiles选项为它们提供服务: app.use(express.static( __dirname+'/static', {dotfiles:'allow'} )); 资源 Looks like the issue happened due to my stupidity. Nodemon did not catch that i added a .folder and did not ...

带有nodejs和express的静态文件夹(Static folder with nodejs and express)

快递3的API是您需要将普通快递模块与您的应用实例分开。 var express = require('express'); //do NOT invoke the function here var app = express(); //DO invoke it here to get app app.use(express.static(__dirname + '/public')); The API as of express 3 is you need the plain express...

nodejs表示不拦截具有静态内容的文件夹的消息(nodejs express not to intercept messages for a folder with static content)

你不能这样做。 默认情况下,节点不提供任何内容 - 在这方面,它与其他一些Web服务器不同。 相反,您通过在中间件堆栈的早期插入正确的中间件命令,专门配置express以直接从文件系统提供来自某些路径的内容。 例如,在我的一个节点应用程序中,我使用此中间件: // static routes that get no further processing app.use('/img', express.static(__dirname + '/img')); app.use('/lib', expr...

Dynamic express.static()文件夹路径(Dynamic express.static() folder path)

尝试从路由处理程序提供静态文件以使其对查询用户名是动态的: app.get('/principal',function(req,res,next){ if(req.query.user){ // serve static files for usename app.use('/principal', express.static(username +'/_site')); res.send("allowed"); }else{ ...

无论如何,Node.js中的Express可以有多个静态文件夹?(Is there anyway Express in Node.js can have more than one static folder?)

this.use(express.static(__dirname + '/public1')); this.use(express.static(__dirname + '/public2')); 第一个将优先考虑文件名。 this.use(express.static(__dirname + '/public1')); this.use(express.static(__dirname + '/public2')); The first one will have priority wit...

Express server静态文件夹不提供文件(Express server static folder does not serve files)

问题在于逗号 - 在__dirname和/public之间。 你应该改为: app.use(express.static(__dirname + '/public')); Uni_Nake建议在公用文件夹之前使用前缀 app.use('/public', express.static(__dirname + '/public')); The problem lies in the comma - , between __dirname and /public. You should chang...

Express js不提供静态文件(Express js does not serve static files)

静态中间件被添加到admin express实例,该实例安装在主app的/admin路由中。 这意味着永远不会为匹配/admin之外的路由调用它。 将中间件移动到主应用实例, app.use(express.static(path.join(__dirname, 'public'))) The static middleware is added to the admin express instance, which is mounted in the main app at the /adm...

Express app定义的`static`文件夹无法正常工作(Express app defined `static` folder not working properly)

选项1 你可以改变这一行: app.use( express.static(__dirname + '/puplic')); //my public folder. 至 app.use('/oneTime', express.static(__dirname + '/puplic')); //my public folder 选项2 我假设你在公共文件夹中有一个js文件夹,然后你需要更改你的HTML代码: <script src="js/angular.min.js"></script> ...

快速父文件夹访问(Express parent folder access)

express.static和res.sendFile对彼此一无所知。 他们碰巧分享了很多相同的内部,但并没有真正相关。 您可以将test.html放在任何位置,然后使用Node的内置path模块引用它。 例如,如果您的文件结构如下所示: test.html real-app/ ├── app.js ├── node_modules/ └── package.json 然后你可以像这样发送test.html : var path = require('path'); // ... app.g...

Node.js Express无法获取静态文件,路由比一个文件夹级别更深(Node.js Express not able to get static files and route is deeper then one folder level)

将<link rel="stylesheet" href="css/main.css">更改为<link rel="stylesheet" href="/css/main.css"> 。 注意前缀/ 。 这将使浏览器开始在树的根目录中搜索,而不是使用相对路径。 Change <link rel="stylesheet" href="css/main.css"> to <link rel="stylesheet" href="/css/main.css">. Note the prefixing /...

相关文章

更多

Working on Free Software

This was written in December, 1999 Lots of programm ...

关于继承和static的小问题

今天帮同事写接口的时候,想到一个问题:现在有2个类 class parent{...} ,class ...

Twitter Storm Real-Life App 排错记

Twitter Storm号称是'实时版本的Hadoop',正好团队在产品中要用,折腾了一下,en,是 ...

全球最流行的66款App的共同规律

根据苹果AppStore、Google Play、App Annie、亚马逊 AppStore及Win ...

Windows Phone 获取app在商店中的版本(检查app的版本号)

public classAppVersionHelper { /// &lt;summary ...

JavaFx Main中怎么绑定Java类中的一个static变量呢?

最近遇到一个很烦的问题,搞了很久搞不定,想请各位帮忙,看看有没有解决的办法,我建了个JavaFx应用程 ...

一个效果很华丽的仿桌面APP,却胜似Launcher

开发Android APP的同学是否对于Launcher实现的绚丽效果而痴迷呢?什么,连Android ...

微信公众平台,移动APP的推广利器

微信公众平台:http://mp.weixin.qq.com/cgi-bin/loginpage?t= ...

在java中如何获取文件夹占用空间的大小?(注意这里不是问文件夹大小)

一般情况下,文件夹大小和文件夹占用空间是不一样的,如附件所示。 在java中,如果要求文件夹大小,可 ...

手机app

手机app是什么? 由于iPhone、三星等智能手机的逐步流行和广泛普及,手机app这个词语开始频繁的 ...

最新问答

更多

绝地求生、荒野行动、香肠派对 哪个更好玩???(都是吃鸡类游戏)

PC上的绝地求生,是最早也是最火的大逃杀游戏。 荒野行动是网易抄袭蓝洞绝地求生制作的手游。相似度90%,还有他一起出的终结折2,这2款正在被蓝洞告,打官司呢。 手游上的绝地求生有2部都是蓝洞授权(收钱)给腾讯开发的正版ID手游。所以跟PC上做的一模一样,蓝洞也没话说。 加上吃鸡国服也是腾讯独家代理,所以根本没有什么可说的。只要这个类型的 过于相似的,腾讯都可以借蓝洞之手起诉。打压同行是国内BAT最爱干的事嘛! 香肠派对画风虽然不一样,但核心玩法还是跟人家正版的一样的,同样也是没有被授权的。 98

如何在jQuery集合中选择第n个jQuery对象?(How to select the nth jQuery object in a jQuery collection?)

你可以使用eq : var rootElement = $('.grid').find('.box').eq(0); rootElement.find('.a'); /* Use chaining to do more work */ You can use eq: var rootElement = $('.grid').find('.box').eq(0); rootElement.find('.a'); /* Use chaining to do more work */

ASP NET使用jQuery和AJAX上传图像(ASP NET upload image with jQuery and AJAX)

您可以自己手动设置FormData键和值。 Upload 创建FormData并设置新的键/值 $("#btnUpload").on("click", function(e) { e.preventDefault(); var file = $("#imguploader").get(0).file

SQL Server XML查询中包含名称空间的位置(SQL Server XML query with namespaces in the where exist)

您可能希望使用#temp.identXml.query而不是#temp.identXml.query 。 您可以在这里阅读更多相关信息SQL Server XML exists() 我相信你也可以像这样使用它 Select #temp.identXml.value('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:SourceIndex)[1]','varchar(100)') as Ident ,#temp.identXml.value(

宁夏银川永宁县望远镇哪里有修mp5的?

胜利街有家电维修,电脑城,银川商场多得很…

我想用更新的日期标记所有更新的行(I would like to mark all updated rows with the date that they have been updated)

您可以使用更新后触发的触发器来执行此操作。 给出如下表: create table your_table (id int primary key, val int, last_update datetime) 每当您更新表中的内容时,此触发器将设置last_update值。 CREATE TRIGGER trigger_name ON your_table AFTER UPDATE AS BEGIN UPDATE your_table SET your_ta

郑州会计培训班

招生的,至于时间吗,就看你自己的时间段了,你可以致电0371-63300220.他们会帮你选择一下的。离你最近,最专业的培训班。

如何定位数组中的负数,并得到所有正数的总和?(How to target e negative number from an array, and get the sum of all positive numbers?)

只需创建一个条件来检查它是正数还是负数,然后定义一个空的数组negatives ,如果数字是负数,则将其推到负数组中,如果是正数,则将其添加到sum变量中,请查看下面的工作示例。 function SummPositive( numbers ) { var negatives = []; var sum = 0; for(var i = 0; i < numbers.length; i++) { if(numbers[i] < 0) { negati

在响应图像上叠加网格(Overlay grid on responsive image)

使用两个linear-gradient s,我们可以创建两个简单的线条,然后每隔n%重复一次background-size 。 它看起来像这样: background: linear-gradient(to bottom, #000 2px, transparent 2px), linear-gradient(to right, #000 2px, transparent 2px); background-size: 10%; 两个渐变创建两条相交的线,长度为百分比,如下所示: 使用默认的b

无法让POST在Azure网站上运行(Could not get POST to work on Azure Website)

最后我找到了答案......我不得不删除尾随的斜线! 我使用了“ https://example.com/api/messages/ ”,这将自动产生GET,无论我使用PostAsync还是PostAsJsonAsync。 使用“ https://example.com/api/messages”,GET和POST似乎都运行良好! Finally I've found the answer.... I had to remove the trailing slash! I've used "ht