简体   繁体   English

将变量从ejs或javascript(浏览器)文件获取到Express

[英]Get the variable from ejs or javascript(browser) file to the Express

How do I take a value from ejs or javascript file to ExpressJS ? 如何从ejs或javascript文件中获取一个值给ExpressJS? I have ejs and javascript files in my project. 我的项目中有ejs和javascript文件。 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. 问题是,当我尝试导入ejs文件时,由于ejs文件具有HTML标签(例如div,body,script等),因此出现语法错误。对于Javascript文件,它有18,000行,是为浏览器编写的。 Meaning, I get thousands of errors like window is not defined , blabla is not a function , x is not defined , etc. 意思是,我得到成千上万的错误,例如未定义windowblabla不是函数x未定义等。

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. 我尝试使用module.exports导出它们,当我尝试将其导入服务器文件(例如'const example = require('./x/index.ejs)'我得到了两个文件的错误。

// 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文件中获取该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM