最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在Node.js中运行JS文件

IT培训 admin 4浏览 0评论

在Node.js中运行JS文件

我是一个自学者,我在Node.js上运行我的测试文件时遇到问题。我使用以下命令:“node”。我不知道文件没有被定义是什么意思。在这种情况下,我正在键入节点test.js.你能帮忙的话,我会很高兴。我面临以下错误:

PS C:\Users\buddys\Desktop\Projects> node test.js
C:\Users\buddys\Desktop\Projects\test.js:8
   document.write("Metri loves you too baby!");
   ^

ReferenceError: document is not defined
    at Object.<anonymous> (C:\Users\buddys\Desktop\Projects\test.js:8:4)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3

现在,当我运行index.html时,JavaScript正在写入网页。这是我的索引和.js文件:

的index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Learning</title>

  </head>
  <body>
    <h1>JS for Metri</h1>
<p>blah blah blah</p>
<div class="content">

</div>

<!--place scripts at the bottom of the body unless big files. If it's a big files
Metri, make a separate .js file and link it to the HTML page by using src attribute. -->
<script src="test.js" charset="utf-8"></script>
  </body>
</html>

test.js

var youLikeMetri = true;
var age= 18;
var enter = 'You are permitted to be legally drunk';
var almost = 'You may legally smoke but not drink'
var exit= 'You are not permitted to be legally drunk';
//if statement if(condition){CODE BLOCK;}
 if (youLikeMetri){
   document.write("Metri loves you too baby!");
 }
//COMPARISON OPERATORS
//==EQUAL TO !=NOT EQUAL TO !==NOT IDENTICAL
//>=GREATER THAN OR EQUAL TO ||OR
//<=LESS THAN OR EQUAL TO &&AND
 if (age >= 21){
   document.write(enter);
 }
//elseif to create multiple conditions after first if.
else if(age <= 20){
  document.write(almost);
}

else if (age < 18){
  document.write(exit);
}
//while loops are continuous if the condition is met.
// while (condition){CODE BLOCK}
while (age <10) {
  console.log('You are less than 10');
  age++
  //adds one to age when age is > 10 it will continue down.
}
document.write('You are older than 10 now')
回答如下:

文档属于Web浏览器中的DOM,因为nodeJS不是客户端javascript,您无法访问浏览器DOM 但是如果你想在客户端应用程序上使用nodeJS模块,你可以使用broserify

在Node.js中运行JS文件

我是一个自学者,我在Node.js上运行我的测试文件时遇到问题。我使用以下命令:“node”。我不知道文件没有被定义是什么意思。在这种情况下,我正在键入节点test.js.你能帮忙的话,我会很高兴。我面临以下错误:

PS C:\Users\buddys\Desktop\Projects> node test.js
C:\Users\buddys\Desktop\Projects\test.js:8
   document.write("Metri loves you too baby!");
   ^

ReferenceError: document is not defined
    at Object.<anonymous> (C:\Users\buddys\Desktop\Projects\test.js:8:4)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3

现在,当我运行index.html时,JavaScript正在写入网页。这是我的索引和.js文件:

的index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Learning</title>

  </head>
  <body>
    <h1>JS for Metri</h1>
<p>blah blah blah</p>
<div class="content">

</div>

<!--place scripts at the bottom of the body unless big files. If it's a big files
Metri, make a separate .js file and link it to the HTML page by using src attribute. -->
<script src="test.js" charset="utf-8"></script>
  </body>
</html>

test.js

var youLikeMetri = true;
var age= 18;
var enter = 'You are permitted to be legally drunk';
var almost = 'You may legally smoke but not drink'
var exit= 'You are not permitted to be legally drunk';
//if statement if(condition){CODE BLOCK;}
 if (youLikeMetri){
   document.write("Metri loves you too baby!");
 }
//COMPARISON OPERATORS
//==EQUAL TO !=NOT EQUAL TO !==NOT IDENTICAL
//>=GREATER THAN OR EQUAL TO ||OR
//<=LESS THAN OR EQUAL TO &&AND
 if (age >= 21){
   document.write(enter);
 }
//elseif to create multiple conditions after first if.
else if(age <= 20){
  document.write(almost);
}

else if (age < 18){
  document.write(exit);
}
//while loops are continuous if the condition is met.
// while (condition){CODE BLOCK}
while (age <10) {
  console.log('You are less than 10');
  age++
  //adds one to age when age is > 10 it will continue down.
}
document.write('You are older than 10 now')
回答如下:

文档属于Web浏览器中的DOM,因为nodeJS不是客户端javascript,您无法访问浏览器DOM 但是如果你想在客户端应用程序上使用nodeJS模块,你可以使用broserify

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论