gpt4 book ai didi

javascript - 无法在 Node.js 中解析 JSON

转载 作者:行者123 更新时间:2023-11-30 16:55:36 25 4
gpt4 key购买 nike

<分区>

我是 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;

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