如何处理node.js / express中的相对路径? [英] How to handle relative paths in node.js / express?

查看:412
本文介绍了如何处理node.js / express中的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在node.js和express中写了一个网站。现在我配置了lighttpd以使用node.js服务器与一个子目录:

  $ HTTP [url] =〜^ / app /{
proxy.server =(=>((
host=>127.0.0.1,
port=> 3000
))

}

当我打开 http:// localhost / app / 我收到错误404,因为我写了这样的东西:

  app.get('/',function(req,res){
res.render('index');
});

有没有更好的方法来修改这些行,如:

  var relPath ='/ app'; 

app.get(relPath +'/',function(req,res){
res.render('index');
});

解决方案

正如Ryan所说,解决方案是:

  app.use('/ app',app.router); 

express.static或express.favicon你还要告诉app.use路径:

  app.use('/ app' ,express.favicon(__ dirname +'/public/images/favicon.ico')); 
app.use('/ app',express.static(__ dirname +'/ public'));

记住在您在html中设置的每个内部链接之前写'/ app'。


I've written a website in node.js and express. Now I configured lighttpd to use the node.js server with an subdirectory:

$HTTP["url"] =~ "^/app/" {
  proxy.server  = ( "" => ( (
                              "host" => "127.0.0.1",
                              "port" => 3000
                            ) )
                  )
}

When I open http://localhost/app/ I get error 404 because I wrote something like this:

app.get('/', function (req, res){
  res.render('index');
});

Is there a better way as modifying these lines like:

var relPath = '/app';

app.get(relPath + '/', function (req, res){
  res.render('index');
});

?

解决方案

As Ryan commented the solution is:

app.use('/app', app.router);

If you use e.g. express.static or express.favicon you have to tell app.use the path also:

app.use('/app', express.favicon(__dirname + '/public/images/favicon.ico'));
app.use('/app', express.static(__dirname + '/public'));

Remember to write '/app' before each internal link you set in your html.

这篇关于如何处理node.js / express中的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆