I'm a bit new to the NodeJS, I'm currently using NodeJS and express to learn how to do a simple web application involving an API call, the front-end is EJS template. template contains a simple form, which allows user to do a post-action, and the middleware will process a simple Goolge Translate API call, and then return the translated results to the front end. Now my question is, how can I persist those form values that user entered?
My initial solution was to use app.locals, so something like below
app.locals.translatedText = "";
app.locals.reqText = "Hello World";
app.locals.sourceLang = "en";
app.locals.targetLang = "es";
Those values would be reset after the API call, and I can access those variables on the EJS template like below;
<input type ="text" id='content' name="content" value="<%=reqText%>">
After some readings, I realised that the variable is at app level, so every user would see the translated result even if they have just load that page. I tried using res.locals, and set the res.locals after the api call, but the problem is that on the EJS template, the res.locals value is not set until the post action is completed, hence, the template will report an error like; translatedText is not found etc.
Appreciate if you could point me to the right direction, is session/cookie more fit for this job?
Regards Jim
Aucun commentaire:
Enregistrer un commentaire