我有这个代码:
res.sendfile( '../../temp/index.html' )
但是,它会抛出此错误:
Error: Forbidden
at SendStream.error (/Users/Oliver/Development/Personal/Reader/node_modules/express/node_modules/send/lib/send.js:145:16)
at SendStream.pipe (/Users/Oliver/Development/Personal/Reader/node_modules/express/node_modules/send/lib/send.js:307:39)
at ServerResponse.res.sendfile (/Users/Oliver/Development/Personal/Reader/node_modules/express/lib/response.js:339:8)
at exports.boot (/Users/Oliver/Development/Personal/Reader/server/config/routes.js:18:9)
at callbacks (/Users/Oliver/Development/Personal/Reader/node_modules/express/lib/router/index.js:161:37)
at param (/Users/Oliver/Development/Personal/Reader/node_modules/express/lib/router/index.js:135:11)
at pass (/Users/Oliver/Development/Personal/Reader/node_modules/express/lib/router/index.js:142:5)
at Router._dispatch (/Users/Oliver/Development/Personal/Reader/node_modules/express/lib/router/index.js:170:5)
at Object.router (/Users/Oliver/Development/Personal/Reader/node_modules/express/lib/router/index.js:33:10)
at next (/Users/Oliver/Development/Personal/Reader/node_modules/express/node_modules/connect/lib/proto.js:199:15)
谁能告诉我为什么会这样?
我已在http-server全球安装.
我在localhost端口8080上从myDir启动它.在myDir中我有index.html.
如果我(从浏览器)请求http://localhost:8080/我得到index.html,这是好的.
如果我请求虽然http://localhost:8080/anything我没有从服务器得到任何响应.
相反,我想要的是我的服务器总是使用index.html响应任何到达端口8080上的localhost的http请求.
这可能吗.
提前致谢
我是JS的新手,我有一个JSON文件,我需要发送到我的服务器(Express),然后我可以解析并在我正在构建的Web应用程序中使用它的内容.
这就是我现在拥有的:
一些糟糕的代码:
app.get('/ search',function(req,res){res.header("Content-Type",'application/json'); res.send(JSON.stringify({/ data.json /})) ;});
在上面的代码中,我只是尝试将文件发送到localhost:3000/search并查看我的JSON文件,但是当我走到那条路径时我收到的是{}.谁能解释一下?
任何帮助都会非常感激.非常感谢提前.
干杯,西奥
data.json中的示例代码段:
[{
    "name": "Il Brigante",
    "rating": "5.0",
    "match": "87",
    "cuisine": "Italian",
    "imageUrl": "/image-0.png"
}, {
    "name": "Giardino Doro Ristorante",
    "rating": "5.0",
    "match": "87",
    "cuisine": "Italian",
    "imageUrl": "/image-1.png"
}]
在我router/index.js,我使用res.sendfile(..)如下:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
    res.sendfile('public/app.html');
});
module.exports = router;
这是我的目录结构:
/Users/lucas/nodeProject/
....app.js
....public/
........app.html
....routes/
........index.js
这个例子的目的是通过http://myurl.com/而不是登录我的页面http://myurl.com/app.html.一切正常,除了我在服务器端控制台上收到以下消息:
express deprecated res.sendfile: Use res.sendFile instead
有谁知道原因,以及如何解决这个问题?简单地替代res.sendfile(..)了res.sendFile(..)给我的错误:
path must be absolute or specify root to res.sendFile
我尝试了其他选项,在此处和此处列出了替代res.sendFile('app.html', { root: path.join(__dirname, '../public') });,但它只给了我这个错误:
ReferenceError: path is not defined
这也是我的依赖项:
"dependencies": …