【问题标题】:Serving files in heroku node js application在heroku node js应用程序中提供文件
【发布时间】:2014-07-10 02:18:07
【问题描述】:

我是节点新手。我已经在 Heroku 中创建并成功测试了节点应用程序。我的文件夹结构是这样的。

app.js(服务器端socket.iofile)。
index.html(公开文件)。

当我通过来自

的 URL 访问应用程序时
Heroku
。 如果我像
appname.herokuapp.com/app.js
这样访问
app.js
文件,我可以访问该文件。有什么方法可以阻止访问此服务器文件?

问候 哈里拉尔

【问题讨论】:

  • 关于您的实际案例的信息太少了:您的 http 服务器如何构建(express/http?),如何加载静态文件的中间件,您的真实目录结构是什么...... ?

标签: javascript node.js heroku socket.io


【解决方案1】:

好吧,您可能正在使用 express.static(),因为它可以访问您目录中的任何静态文件。无论如何,您可以为 app.js 创建一个路由:

var app=express();
app.use(app.router);            //this will make sure that node.js will check for a route  before checking for app.js file and displaying it.
app.use(express.static(__dirname+'/directoryPath'));


//now set the route for app.js 

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

 //what-ever you want should happen when u go to appname.herokuapp.com/app.js
});

【讨论】:

    【解决方案2】:

    其实我没有使用“快递”。 我的代码是这样的 http://pastebin.com/7BcZZsMk

    我的文件夹结构如下:

    app
    ----css
    --------style.css
    ----images
    --------cup.png
    --------tree.png
    ----app.js
    ----index.html
    

    【讨论】:

      猜你喜欢
      • 2015-03-29
      • 1970-01-01
      • 2018-01-27
      • 2017-11-12
      • 2013-07-23
      • 1970-01-01
      • 2015-03-08
      • 2014-12-13
      • 2015-11-10
      相关资源
      最近更新 更多