发送指定网址的静态文件 [英] Send a static file for a specifed URL
问题描述
我当前正在使用解析来部署我的Web应用程序.我正在使用Express将URL路由到适当的内容.
I am currently using parse to deploy my web app. I am using Express to route the url to the appropriate content.
当前,我尝试为以下网址提供index.html;
Currently I trying to serve index.html for the following url;
mystackoverflowapp.parseapp.com/hi
在我的云代码中,我具有以下请求处理程序
In my cloud code I have the following request handler
app.get('/hi', function(req, res){
res.sendFile('index.html');
});
根据分析文档,默认目录为公共目录.不幸的是,此请求处理程序不起作用.当我转到stackoverflow.parseapp.com时,出现500错误内部服务器错误".
According to the parse documentation the default directory is the public directory. Unfortunately this request handler does not work. When I go to my stackoverflow.parseapp.com I get a 500 error, "Internal Server Error".
我正在遵循与文档中概述的相同的目录结构,并尝试在每个目录中都包含index.html文件.
I am following the same directory structure as outlined in the documentation and have tried having the index.html file in every directory.
如果我尝试
res.send('This works!')
对于相同的路线,我得到了预期的响应,
for the same route I get the expected response,
"This works!".
对我在做什么错有任何想法吗?另外,对此进行更好的调试的最佳方法是什么? 500错误并不能说明太多.
Any thoughts on what I am doing wrong? Also, what's the best way to get better debugging on this? A 500 error doesn't really reveal too much.
推荐答案
来自 http: //expressjs.com/4x/api.html#res.sendFile :
除非在options对象中设置了root选项,否则path必须是 文件的绝对路径.
Unless the root option is set in the options object, path must be an absolute path of the file.
更新1:我尝试过:
app.get('/test', function(req,res) {
try {
res.sendFile('test.html');
} catch (err) {
res.send('ERROR ' + err);
}
});
它产生:
错误TypeError:对象[object Object]没有方法'sendFile'
ERROR TypeError: Object [object Object] has no method 'sendFile'
然后我偶然发现了以下内容:如何在Parse上手动提供文件.com?.相关报价:
I then stumbled across this: How to manually serve files on Parse.com?. The relevant quote:
...似乎Parse运行Node模块的自定义子集.他们已删除 所有文件系统方法. Express API上没有sendFile(),没有 fs模块上的readFile()...
...it seems Parse runs a custom subset of Node modules. They've erased all filesystem methods. There's no sendFile() on Express API, no readFile() on fs module...
更新2:
Parse为您的应用程序设置了一个静态文件处理程序,因此请尝试以下操作:
Parse sets up a static file handler for your app, so try this:
app.get('/hi', function(req, res){
res.redirect('/index.html');
});
这篇关于发送指定网址的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!