Angularjs 使用Express服务Angular应用程序

Angularjs 使用Express服务Angular应用程序,angularjs,node.js,express,Angularjs,Node.js,Express,我正在使用节点上的Express构建一个Angular应用程序 我想做的是,当用户访问该站点时,将其区域设置附加到url(domain.com/en gb) 然后,不管他们指定了什么语言环境,都会提供相同的index.html app.use('/', function(req, res) { res.sendFile(__dirname + '/public/index.html'); }); 我遇到的问题是,如何在不考虑请求的情况下提供相同的文件,但允许图像等资产也不被重新路由到in

我正在使用节点上的Express构建一个Angular应用程序

我想做的是,当用户访问该站点时,将其区域设置附加到url(domain.com/en gb)

然后,不管他们指定了什么语言环境,都会提供相同的index.html

app.use('/', function(req, res) {
   res.sendFile(__dirname + '/public/index.html');
});
我遇到的问题是,如何在不考虑请求的情况下提供相同的文件,但允许图像等资产也不被重新路由到index.html

谢谢,
Harry

如果您添加一个类似以下语句的
use
语句,则您可以指定图像的位置:

var staticPathImages = __dirname + '/assets/images';
app.use(express.static(staticPathImages));

express将直接为这些静态资产提供服务,而无需重新路由到
index.html
..

如果MarcoS回答正确,我将添加更详细的描述:

app.use(express.static(path.join(__dirname, 'public'))); //this line enables the static serving in the public directory
您的
public
目录可以是以下目录:

public 
|_css
|_js
|_images
  |_logo.png
然后,如果要获取图像:

http://localhost:3000/images/logo.png
如果您想在html页面中显示它:

<img id="logo" src="/images/logo.png"/>


确保您有备用方案,我将其设置为:var staticPathImages=\uu dirname+'/public/assets';app.use(express.static(staticPathImages));app.use('/',function(req,res){res.sendFile(uu dirname+'/public/index.html');});但它仍然为每个文件提供index.html,知道我做错了什么吗?答案是“是”…:-)如果您请求
localhost:3000/public/assets/image.jpg
(或类似文件),是否确定提供index.html文件?听起来很奇怪。。。我有一个类似于你的设置,它工作得很好…:-)你重新启动服务器了吗?也许它正在运行你的旧代码。终于,它开始工作了,只是必须纠正路径!谢谢你的帮助!:)