Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

javascript - 由于无效的JSON,NodeJS Firebase应用程序崩溃(NodeJS Firebase App crash on Invalid JSON)

This is my NodeJS application.(这是我的NodeJS应用程序。)

I've commented out some parts to narrow down the issue.(我已注释掉某些部分以缩小问题范围。) const functions = require('firebase-functions'); const express = require('express'); // const path = require('path'); // const bodyParser = require('body-parser'); const app = express(); // const gameAPI = require('./routes/apiv1'); const startTime = Date.now(); Error.stackTraceLimit = Infinity; console.log("Server Started"); // app.use('/node_modules',express.static(path.join(__dirname, 'node_modules'))); // app.use(bodyParser.json()); // app.use(bodyParser.urlencoded({ extended: true })); app.use((err, req, res, next) => { res.status(500).send(); }); // app.use('/v1.0', gameAPI); app.get('/', (req, res)=>{ res.send(`App started ${(Date.now() - startTime)/1000}s ago`); }); exports.app = functions.https.onRequest(app); when I send an invalid JSON(当我发送无效的JSON时) { "ign":"XX" "allies": "5" } The application crash.(应用程序崩溃。) [11:21:31]{}firebase-test:sulochana$ firebase serve --only functions,hosting ? functions: Using node@8 from host. ? functions: Emulator started at http://localhost:5001 i functions: Watching "/home/sulochana/Documents/firebase-test/functions" for Cloud Functions... i hosting: Serving hosting files from: public ? hosting: Local server: http://localhost:5000 > Server Started ? functions[app]: http function initialized (http://localhost:5001/scrim-engine/us-central1/app). [hosting] Rewriting /v1.0/dota/queue to http://localhost:5001/scrim-engine/us-central1/app for local Function app > Server Started i functions: Beginning execution of "app" > SyntaxError: Unexpected string in JSON at position 15 > at JSON.parse (<anonymous>) > at parse (/usr/lib/node_modules/firebase-tools/node_modules/body-parser/lib/types/json.js:89:19) > at /usr/lib/node_modules/firebase-tools/node_modules/body-parser/lib/read.js:121:18 > at invokeCallback (/usr/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:224:16) > at done (/usr/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:213:7) > at IncomingMessage.onEnd (/usr/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:273:7) > at emitNone (events.js:111:20) > at IncomingMessage.emit (events.js:208:7) > at endReadableNT (_stream_readable.js:1064:12) > at _combinedTickCallback (internal/process/next_tick.js:139:11) How to handle this exception.(如何处理此异常。) This will allow external users to crash the application(这将允许外部用户使应用程序崩溃)   ask by heysulo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to determine where the JSON.parse() method is being invoked and then proceed it with a test for whether the JSON is valid before trying to use the parsed output.(您必须确定在何处调用JSON.parse()方法,然后尝试使用解析后的输出之前对其进行测试,以测试JSON是否有效。)

For example:(例如:) const isValidJson = jsonString => { try { JSON.parse(jsonString) return true } catch(err) { return false } } if (isValidJson(jsonString)) JSON.parse(jsonString) The likeliest culprit for where this parsing is happening appears to be in your game API, since it seems to be handling your app's routes.(进行解析的最可能的罪魁祸首似乎在您的游戏API中,因为它似乎正在处理您应用的路由。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...