10 examples of 'express.json vs bodyparser.json' in JavaScript

Every line of 'express.json vs bodyparser.json' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
5export function bodyJson() {
6 return json();
7}
12use(req: Request, res: Response, next: NextFunction) {
13 bodyParser.json(this.options)(req, res, next);
14}
87function jsonEncodedParser(body) {
88 if (jsonObjRegex.test(body)) {
89 /* eslint-disable no-empty */
90 try {
91 return JSON.parse(body)
92 } catch (error) {
93 // Do nothing
94 }
95 /* eslint-enable no-empty */
96 }
97 throw httpError(400, 'POST body sent invalid JSON.')
98}
8function jsonBody(req, res, opts, callback) {
9 var args = parseArguments(req, res, opts, callback)
10 req = args.req
11 res = args.res
12 opts = args.opts
13 callback = args.callback
14
15 if (!callback) {
16 return jsonBody.bind(null, req, res, opts)
17 }
18
19 var parse = opts.JSON ? opts.JSON.parse : jsonParse
20 var reviver = opts.reviver || null
21
22 body(req, res, opts, function (err, body) {
23 if (err) {
24 return callback(err)
25 }
26
27 parse(body, reviver, callback)
28 })
29}
17function parse (json) {
18 try {
19 callback(null, JSON.parse(body))
20 } catch (err) {
21 callback(err, null)
22 }
23}
71function parse (body) {
72 if (body.length === 0) {
73 // special-case empty json body, as it's a common client-side mistake
74 // TODO: maybe make this configurable or part of "strict" option
75 return {}
76 }
77
78 if (strict) {
79 var first = firstchar(body)
80
81 if (first !== '{' && first !== '[') {
82 debug('strict violation')
83 throw new SyntaxError('Unexpected token ' + first)
84 }
85 }
86
87 debug('parse json')
88 return JSON.parse(body, reviver)
89}
205public setJsonBody(json: any) {
206 this._jsonBody = json;
207}
100export function* parseBody(ctx, options, next) {
101 const fields = typeof options.fields === 'string' ? options.fields : 'fields';
102 const files = typeof options.files === 'string' ? options.files : 'files';
103 const {
104 custom
105 } = options.extendTypes;
106
107 if (custom && custom.length > 0 && ctx.request.is(custom)) {
108 yield* options.handler.call(ctx, ctx, options, next);
109 return yield* next;
110 }
111
112 if (options.detectJSON(ctx) || ctx.request.is(options.extendTypes.json)) {
113 ctx.app.jsonStrict = typeof options.jsonStrict === 'boolean' ? options.jsonStrict : true;
114 ctx.request[fields] = yield ctx.request.json(options.jsonLimit);
115 return yield* next;
116 }
117
118 if (ctx.request.is(options.extendTypes.form || options.extendTypes.urlencoded)) {
119 const res = yield ctx.request.urlencoded(options.formLimit);
120 ctx.request[fields] = res;
121 return yield* next;
122 }
123
124 if (options.buffer && ctx.request.is(options.extendTypes.buffer)) {
125 ctx.request.body = yield ctx.request.buffer(options.bufferLimit);
126 return yield* next;
127 }
128
129 if (ctx.request.is(options.extendTypes.text)) {
130 const limit = options.textLimit;
131 const body = yield ctx.request.text(limit);
132 ctx.request.body = body;
133 return yield* next;
134 }
135
136 if (options.multipart && ctx.request.is(options.extendTypes.multipart)) {
137 const result = yield ctx.request.multipart(options);
138 ctx.request[fields] = result.fields;
139 ctx.request[files] = result.files;
140 return yield* next;
141 }
142}
14function body (json) {
15 var result = {};
16
17 result.list = json;
18
19 result.list = _.map(result.list, function (item) {
20 item.exclude = _.indexOf(obj.excludeValues, item.id) !== -1;
21
22 return item;
23 });
24
25 return templateService.get("grails-admin-list", result);
26}
90function jsonBody(json, options) {
91 return {
92 body: JSON.stringify(json),
93 headers: {
94 ...R.defaultTo({}, R.prop('headers', options)),
95 'Content-Type': 'application/json',
96 },
97 }
98}

Related snippets