Running Grunt Express Server causes Error: ENOENT: no such file or directory, open 'static/test.json'

Running Grunt Express Server causes Error: ENOENT: no such file or directory, open 'static/test.json'

我有一个正在更新的当前应用程序以使用 Express Node.js 库。我已经更新了 Grunt.js 任务以使用 grunt-express-server 包。我的部分代码正在尝试访问 JSON 文件的内容(请参阅下面的目录结构)。 运行 服务器很好,但是当我尝试访问任何静态文件时出现以下错误:

Error: ENOENT: no such file or directory, open 'static/test.json'
    at Error (native)
    at Object.fs.openSync (fs.js:549:18)
    at Object.fs.readFileSync (fs.js:397:15)
    at /Users/owen/src/projects/node-express-cannot-get/build/index.js:13:32
    at Layer.handle [as handle_request] (/Users/owen/src/projects/node-express-cannot-get/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/owen/src/projects/node-express-cannot-get/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/Users/owen/src/projects/node-express-cannot-get/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/owen/src/projects/node-express-cannot-get/node_modules/express/lib/router/layer.js:95:5)
    at /Users/owen/src/projects/node-express-cannot-get/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/Users/owen/src/projects/node-express-cannot-get/node_modules/express/lib/router/index.js:330:12)

我认为这可能与我的目录结构以及 Express 服务器 运行 的来源有关。我的 G运行t 任务将所有源文件从名为 src 的文件夹(其中包含我的 index.js)复制到名为 build[ 的文件夹中=36=] 然后 运行s 来自根目录中的 G运行t 文件的服务器。如果我 cd 进入构建文件夹并直接 运行 index.js (node index.js),错误消失。但是,运行通过 G运行t 任务对其进行处理会产生错误。

如何在不更改目录结构的情况下解决此问题?有没有办法让 G运行t Express Server 任务改变它运行 的来源?或者这个问题还有其他原因吗?

请参阅下面我的目录结构和代码示例:

目录结构(G运行t任务完成后)

/
  build/
    static/
      test.json
    index.js
  node_modules/
  src/
    static/
      test.json
    index.js
  Gruntfile.js
  package.json

Gruntfile.js

module.exports = function (grunt) {
    require('load-grunt-tasks')(grunt);
    grunt.config.merge({
        clean: {
            dev: ['build']
        },
        mkdir: {
            build: {
                options: {
                    mode: 0755,
                    create: ['build']
                }
            }
        },
        copy: {
            dev: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: ['index.js', 'static/**/*'],
                    dest: 'build'
                }]
            }
        },
        express: {
            dev: {
                options: {
                    'script': 'build/index.js',
                    'port': 2000
                }
            }
        },
        watch: {
            index: {
                files: ['index.js'],
                tasks: ['clean', 'mkdir', 'copy']
            }
        }
    });
    grunt.registerTask('default', [
        'clean:dev',
        'mkdir:build',
        'copy:dev',
        'express:dev',
        'watch:index'
    ]);
};

src/index.js

var express = require('express');
var path = require('path');
var fs = require('fs');

var app = express();

app.set('port', (process.env.PORT || 2000));

app.use('/', express.static(path.join(__dirname, '/')));

app.get('/', function (req, res) {
    configurationPath = 'static/test.json';
    // I beleive this is where the problem happens...
    var configurationJSON = fs.readFileSync([configurationPath].join("")).toString();
    console.log('configurationJSON', configurationJSON);
    res.send('Hello world');
});

app.listen(app.get('port'), function () {
    console.log('It\'s all go on port: ' + app.get('port') );
});

在 index.js 中对 readFileSync 的调用失败,因为它传递了一个相对路径 - 'static/test.json' - 而当前目录不是你想的那样。

当前目录是包含 package.json 的目录 - 不是 build 目录。

如果您使用 __dirname - 正如您对 static 中间件所做的那样 - 它应该可以解决您的问题:

var configurationPath = path.join(__dirname, 'static/test.json');