【发布时间】:2023-03-10 04:40:01
【问题描述】:
我是一个 node.js 初学者,我正在尝试读取一个 json 文件,但是当我在终端中运行“npm start”时,我收到了这个错误:
undefined:3462
SyntaxError: Unexpected end of input
at Object.parse (native)
at /Users/alonbond/node_apps/analoc_2/analoc/routes/index.js:15:20
at fs.js:334:14
at FSReqWrap.oncomplete (fs.js:95:15)
这是 index.js:
var express = require('express');
var fs = require('fs');
var app = express.Router();
/* GET home page. */
app.get('/', function(req, res, next) {
console.log('Welcome to Express.js');
res.render('index', { title: 'Express' });
});
/* GET json */
app.get('/analoc/', function(req, res) {
fs.readFile('./sample_data.json', function(error, data){
jsonObj = JSON.parse(data);
res.send('THE DATA: ', jsonObj);
});
});
module.exports = app;
有什么帮助吗? 谢谢!
【问题讨论】:
-
您确定文件存在于该路径并且文件中有内容?
-
是的。我已经检查了文件
-
顺便说一句,你可以简单的
requirejson 文件。