Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 26, 2022 06:44 am GMT

How to access submitted form data value from request in ExpressJS

Project: Codever - File: user.router.js

The values are present in the request.body which contains key-value pairs of data submitted in the request body.

In our case we access the userDisplayName with the following expression request.body.userDisplayName as in the example below:

usersRouter.post('/:userId/bookmarks/upload', keycloak.protect(),  uploadBookmarks.single("bookmarks" /* name attribute of <file> element in your form */),  async (request, response) => {    userIdTokenValidator.validateUserId(request);    const userDisplayName = request.body.userDisplayName;    const importResponse = await browserBookmarksImportService.imporBrowserBookmarks(request.params.userId, request.file.buffer, userDisplayName);    const str = JSON.stringify(importResponse, null, 2); // spacing level = 2    console.log(str);    return response.status(HttpStatus.OK).send(importResponse);  });

In angular the userDisplayName value has been appended to the FormData and submitted to a post request
via the Angular Http Client:

  uploadBookmarks(userId: String, bookmarks: File, userDisplayName: string): Observable<any> {    const formData = new FormData();    formData.append('bookmarks', bookmarks);    formData.append('userDisplayName', userDisplayName);    return this.httpClient.post(`${this.usersApiBaseUrl}/${userId}/bookmarks/upload`, formData);  }


Reference -

https://expressjs.com/en/api.html#req.body

Shared with from Codever. Use copy to mine functionality to add it to your personal snippets collection.


Original Link: https://dev.to/codever/how-to-access-submitted-form-data-value-from-request-in-expressjs-knf

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To