In my next.js + express.js (custom server within next) app, there is a flow of follows:
- user selects a parameter with drop down menu.
- upon her/his selection of parameter, a process is executed in the backend. 2.1. upon the parameter, I define the a filePath, like filePath = "myPath/parameter/coords.js"
- when the process is finished, it creates a file and writes some coordinates info.
- when the process is finished I "require(file)" and will be showing the coordinates on my leaflet comp.
Therefore I need something like this
if (processIsFinished) {
const coords = require(filePath)
}
My questions are:
I cannot define filePath with a variable. When I say /Users/..../parameter/coords.js, it is ok, no error (if there is already a file, which refers to my question-2) but since parameter is selected by user, I want the filePath defined as a var. I tried:
const coords = require(filePath)
const coords = require(
${filePath}
)const coords = require(
myPath/${parameter}/coords
)const coords = require('' + filePath)
all did not work... how can we define the path in require with a variable?
- when there is no file, the "require" throws error, even if it is put in a conditional statement block (if statement). how can we make it work, like, if the "processIsFinished" then execute the require statement, because then there will be such a file.
Thanks...