gpt4 book ai didi

javascript - 运行 Grunt Express Server 会导致错误 : ENOENT: no such file or directory, open 'static/test.json'

转载 作者:行者123 更新时间:2023-12-02 14:03:17 24 4
gpt4 key购买 nike

我有一个当前应用程序,正在更新以使用 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 服务器的运行位置有关。我的 Grunt 任务将所有源文件从名为 src 的文件夹(其中包含我的 index.js)复制到名为 build 的文件夹,然后从 Gruntfile 运行服务器,该文件夹是在根目录中。如果我cd进入构建文件夹并直接运行index.js (node index.js),错误就会消失。但是,通过 Grunt 任务运行它会产生错误。

如何在不更改目录结构的情况下解决此问题?有没有办法让 Grunt Express Server 任务更改它的运行位置?还是有其他原因导致此问题?

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

目录结构(Grunt 任务完成后)

/
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');

关于javascript - 运行 Grunt Express Server 会导致错误 : ENOENT: no such file or directory, open 'static/test.json',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40238362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com