1

For some reason, my express server is not loading the file system 'fs' module correctly. I'm using the angular-fullstack yeoman generator. My system is Windows 7 with node version 0.10.35, npm version 2.1.18, and the latest version of angular-fullstack. I've tried all sorts of things, like both 32-bit and 64-bit and updating everything.

routes.js (which has other routes that load fine):

'use strict';

var errors = require('./components/errors');
var express = require('express');
var fs = require('fs');

module.exports = function(app) {

    app.route('/pdf/*')
        .get(function(req, res) {
            var pdfPath = app.get('appPath') + '/assets/pdf/test.pdf';
            fs.readfile(pdfPath, function(error, data) {
                res.setHeader('Content-Disposition', 'attachment; filename="test.pdf"');
                res.setHeader('Content-Type', 'application/pdf');
                res.setHeader('Content-Length', data.length);
                res.status(200).end(data, 'binary');
            });
        });

    // All undefined asset or api routes should return a 404
    app.route('/:url(api|auth|components|app|bower_components|assets)/*')
        .get(errors[404]);    

    // All other routes should redirect to the index.html
    app.route('/*')
        .get(function(req, res) {
            res.sendfile(app.get('appPath') + '/index.html');
        });
};

Server error:

TypeError: Object #<Object> has no method 'readfile'
    at Object.handle (C:\Projects\policy5\server\routes.js:19:7)
    at next_layer (C:\Projects\policy5\node_modules\express\lib\router\route.js:103:13)
    at Route.dispatch (C:\Projects\policy5\node_modules\express\lib\router\route.js:107:5)
    at c (C:\Projects\policy5\node_modules\express\lib\router\index.js:195:24)
    at Function.proto.process_params (C:\Projects\policy5\node_modules\express\lib\router\index.js:251:12)
    at next (C:\Projects\policy5\node_modules\express\lib\router\index.js:189:19)
    at next (C:\Projects\policy5\node_modules\express\lib\router\index.js:166:38)
    at trim_prefix (C:\Projects\policy5\node_modules\express\lib\router\index.js:228:11)
    at c (C:\Projects\policy5\node_modules\express\lib\router\index.js:198:9)
    at Function.proto.process_params (C:\Projects\policy5\node_modules\express\lib\router\index.js:251:12)
GET /pdf/test.pdf 500 2ms - 934b

It's readFile() not readfile() (uppercase F vs lowercase f).

4

1 回答 1

4

readFile()不是readfile()(大写F与小写f)。

于 2015-01-12T02:27:26.480 回答