node.js - Node.js 上的 getElementById 函数

标签 node.js

我正在尝试构建一个 node.js 应用程序,我只是尝试在 window.onload 中插入一个 getElementById 函数。当我打算制作 node index.js 下面的代码时;

var http = require('http');

function handler(req, res) {

  var form = '';
  if (req.method == "GET") {

    form = '<!doctype html> \
    <html lang="en"> \
    <head> \
        <meta charset="UTF-8">  \
        <meta http-equiv="X-UA-Compatible" content="text/html"> \
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> \
        <title>XYZ HR</title> \
    </head> \
    <body> \
      <form name="myForm" action="" onsubmit="return ajax();"method="post">\
          <br> \
          <input type="file" id="myResume" accept=".txt,.doc,.docx,.pdf,.jpg"> \
      </form> \
      <hr> \
      <button type="submit" id="submitBTN" onclick="">Submit</button> \
      <div id="displayResume"></div> \
      <script src="http://code.jquery.com/jquery-latest.min.js"></script> \
      <script> \
        window.onload = function () { \
            var myResume = document.getElementById('
    myResume '); \

Is that not working on node.js to define elements server-side? It produce an error on the terminal like below and I couldn't find a proper example with same error.

/Users/erhanyasar/Documents/Web/www/arete/HR/node/index.js:30
    var myResume = document.getElementById('myResume'); \
                                            ^^^^^^^^
SyntaxError: Unexpected identifier
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:423:7)
    at startup (bootstrap_node.js:147:9)

最佳答案

嗯,在nodejs中服务端渲染没有问题。只需使用任何模板引擎即可,例如 http://olado.github.io/doT/index.html 。他们有关于如何做到这一点的很好的例子。

关于node.js - Node.js 上的 getElementById 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44463507/

相关文章:

javascript - 无法使用 Node.js 中的请求模块传输大尺寸文件

服务器上的 JSON 解析与解析客户端

node.js - 使用 node.js 的 Mongoose 中的日期转换错误

javascript - 在 Elasticsearch 中进行搜索查询时忽略存储数据中的特殊字符

node.js - 使用快速验证器 typescript 定义

node.js - 将 Postgres 与新创建的 React 应用程序一起使用时出错 : Module not found: Can't resolve 'dns'

javascript - 多个无限循环

javascript - NodeJS 中未从 MySQL 查询获取返回对象

javascript - 如何抑制浏览器中的 javascript 错误?

javascript - 为什么sinon认不出我的 stub ?