简体   繁体   English

如果我提供静态文件,在快速路由器中,方法将不起作用

[英]In express router method is not working if i serve static files

In my node application first i want to serve my static files and then using router method depending upon the incoming request i will query the DB and get the body of the html page then i will send this body content to the client side and finally using Backbone.js i will render this body in my html page.Now what is my problem means express router method is not working if i serve static files.But if i remove the below line means my router is working. 在我的节点应用程序中,我首先要提供我的静态文件,然后根据传入的请求使用路由器方法,我将查询数据库并获取html页面的正文,然后将这些正文内容发送给客户端,最后使用Backbone .js我将在我的html页面中呈现此主体。现在,我的问题是如果我提供静态文件,则表示路由器方法不起作用。但是,如果我删除以下行,则表明路由器正在工作。

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

app.js app.js

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

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

app.get('/',function(req,res){

   console.log('router called successfully...');
   res.send('body of the page');
   res.end();

});

app.listen(8011);
app.use(express.static(__dirname + '/public'));

Your unnecessary anonymous function is screwing up how the middleware is supposed to work. 您不必要的匿名功能正在搞乱中间件应该如何工作。 The above line is in 95% or more of all express sample apps. 上面的代码包含在所有快速示例应用程序中的95%或更多。 Not sure why you decided to deviate from that, but your version is a useless middleware that creates a static middleware and immediately discards it. 不知道为什么您决定偏离这一点,但是您的版本是一个无用的中间件,它会创建一个静态中间件并立即将其丢弃。

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

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