res.sendfile()不能很好地提供javascripts [英] res.sendfile() doesn't serve javascripts well

查看:121
本文介绍了res.sendfile()不能很好地提供javascripts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用静态文件服务而不使用任何渲染引擎。
我试图使用:

I want to use static file serve without any rendering engine. I've tried to use:

res.sendfile('public/index.html');

,并为我的路由上的静态文件表达中间件:

on the '/' GET route, and express middleware for static files on my route:

app.use(express.static(path.join(__dirname, 'public')));

但是,似乎客户端要求的所有javascript都是使用index.html文件信息下载

BUT it seems like all of the javascripts which the client asks for are downloaded with index.html file information.

如何成功下载CSS / JS静态文件?

How can I make a successful download of the CSS/JS static files ?

更新:

这是res.sendfile ...的路由:

Here is the route for the "res.sendfile ..." :

app.get('/*', index);

我希望在任何路线上向服务器发出的所有请求都将获得 index .html 及其所有JS& CSS与。

I want all of the requests to the server on any route will get index.html and all of its JS&CSS assosiciated with.

推荐答案

我想这可能会帮助你...

I guess this might help you...

app.js 文件...

app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

app.use("/styles",  express.static(__dirname + '/public/stylesheets'));
app.use("/scripts", express.static(__dirname + '/public/javascripts'));
app.use("/images",  express.static(__dirname + '/public/images'));


// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', function (req, res) {
      res.sendfile(__dirname + '/public/home.html');
});

home.html code> / public 文件夹和 / public / javascripts 中的JavaScript文件, / public / images ,css文件在 / public / stylesheets 文件夹中。

save the home.html inside the /public folder and JavaScript files in /public/javascripts, images in /public/images, css files in /public/stylesheets folder.

在HTML文件中,作为您定义的单词(例如:/ 脚本 /home.js)...像这样

In the HTML file reference should be the words you define(eg: /scripts/home.js)... like this

    <link rel="stylesheet"   type="text/css" href="/styles/home.css" >
    <script src="/scripts/home.js" type="text/javascript"></script>   

这篇关于res.sendfile()不能很好地提供javascripts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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