为URL提供文件

时间:2015-09-22 07:14:35

标签: javascript node.js express parse-platform

我正在尝试为特定网址手动提供文件。

当我使用res.sendFile函数时,我的日志中出现以下错误。

Object [object Object] has no method 'sendFile'

Express version : 4.13.3

似乎无法使用此功能。有一个关于在文档中提供静态内容的部分,但是您将如何提供它呢?

更新的代码:

express = require('express');
app = express();

app.set('views', 'cloud/views');  // Specify the folder to find templates
app.set('view engine', 'ejs');    // Set the template engine
app.use(express.bodyParser());    // Middleware for reading request body
app.get('/hi', function(req, res){
  res.sendFile('index.html', function (err) {
    if (err) {
      console.log(err);
    }
    else {
      console.log('File sent!');
    }
  });
});

app.listen();

错误日志

I2015-09-22T07:08:37.766Z]v39 Ran custom endpoint with:
  Input: {"method":"GET","url":"/hi","headers":{"accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","accept-encoding":"gzip, deflate, sdch","accept-language":"en-US,en;q=0.8,fr;q=0.6,sq;q=0.4","cache-control":"no-cache","host":"......parseapp.com","pragma":"no-cache","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36","version":..........."}}
  Result: Success
I2015-09-22T07:08:37.855Z]TypeError: Object [object Object] has no method 'sendFile'
    at app.js:13:7
    at callbacks (express_router.js:161:37)
    at param (express_router.js:135:11)
    at pass (express_router.js:142:5)
    at Router._dispatch (express_router.js:170:5)
    at Object.router (express_router.js:33:10)
    at next (connect_proto.js:240:15)
    at urlencoded (connect_urlencoded.js:51:37)
    at connect_bodyParser.js:64:7
    at json (connect_json.js:53:37)

1 个答案:

答案 0 :(得分:0)

可以将代码与

进行比较
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
app = express();

app.set('views', 'cloud/views');  // Specify the folder to find templates
app.set('view engine', 'ejs');    // Set the template engine
app.use(bodyParser.json());    // Middleware for reading request body
app.get('/hi', function(req, res){
  res.sendFile(path.join(__dirname + '/index.html'), function (err) {
    if (err) {
      console.log(err);
    }
    else {
      console.log('File sent!');
    }
  });
});

app.listen(3100);