How do I take a value from ejs or javascript file to ExpressJS ? I have ejs and javascript files in my project. Both of these files have the same variable, so I need to find a way to get that variable from one of them. Problem is, when I try to import ejs file, I get syntax errors because ejs file has HTML tags such as div, body, script, etc. For the Javascript file, it has 18,000 lines and it was written for browsers. Meaning, I get thousands of errors like window is not defined , blabla is not a function , x is not defined , etc.
I tried to export them with module.exports, when I try to import it on server file like 'const example = require('./x/index.ejs)'
then I get the errors for both of the files.
// The thing that I want an example
// Server side
const x = require('./x/file)'
app.get('/havaist/live', (req, res) => {
console.log(x.theWantedVariable)
})
// Ejs
<body>
<div> <h1> Hey there. </h1> </div>
<script>
.
..
...
....
here the data that needs get fetched from ExpressJS.
....
...
..
.
</script>
</body>
//Javascript
.
..
...
....
here the data that needs get fetched from ExpressJS.
....
...
..
.
ERRORS:
When trying to import EJS:
/home/nimda/Documents/Devspace/hikvision-api/views/index.ejs:1
(function (exports, require, module, __filename, __dirname) { <body onload="getCameraNo();realplay();">
^
SyntaxError: Unexpected token <
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
When trying to import JAVASCRIPT:
/home/nimda/Documents/Devspace/hikvision-api/views/dist/jsPlugin-1.2.0.min.js:10
})(window, function() {
^
ReferenceError: window is not defined
at Object.<anonymous> (/home/nimda/Documents/Devspace/hikvision-api/views/dist/jsPlugin-1.2.0.min.js:10:4)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/home/nimda/Documents/Devspace/hikvision-api/server.js:9:13)
at Module._compile (internal/modules/cjs/loader.js:689:30)
从ejs文件发送ajax POST请求,并从express文件中获取该值。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.